gpt4 book ai didi

Java变量覆盖

转载 作者:行者123 更新时间:2023-11-30 06:12:53 25 4
gpt4 key购买 nike

我有一个带有 for 的程序,它从 Arraylist 中获取字符串,拆分它们并将它们发送到 Worker 类。

worker 类(Class):

public class WorkerRSR extends SwingWorker<String, Void>{
private static String urlFrames;
private static String urlImg;
public static int bool;
public static int dist;
public static int numI;
public static int spra;
public static boolean isCoda;
public static int numCoda;
public static String algo;

public WorkerRSR(String urlImg, int dist, int numI, int spra, String algo, String urlFrames, boolean isCoda, int numCoda) {
this.urlImg=urlImg;
this.dist=dist;
this.numI=numI;
this.spra=spra;
this.algo=algo;
this.urlFrames=urlFrames;
this.isCoda = isCoda;
this.numCoda = numCoda;

//FIRST CHECK POINT

}

@Override
protected String doInBackground() throws Exception {
PanelRSR_LRSR.getProgessbar().setIndeterminate(true);
go();
return "";
}

@Override
protected void done() {
System.out.println("Algoritmo RSR esguito");
if(isCoda){
CreateOption.codaCont++;
System.out.println("RSR codaCont: "+CreateOption.codaCont);
if(CreateOption.codaCont==CreateOption.csize){
JOptionPane.showMessageDialog(null,"Coda Eseguita", "Attenzione",JOptionPane.WARNING_MESSAGE);
PanelRSR_LRSR.getProgessbar().setIndeterminate(false);
}
}
else{
PanelRSR_LRSR.getProgessbar().setIndeterminate(false);
JOptionPane.showMessageDialog(null,"Finito RSR", "Attenzione",JOptionPane.WARNING_MESSAGE);
}
}

public static void go() throws IOException{
System.out.println("ESEGUO RSR, attendi...");

//SECOND CHECK POINT

System.out.println("RSR n = "+numI+" codaCont: "+CreateOption.codaCont+" numCoda = "+numCoda);
while(true){
if(numCoda==CreateOption.codaCont)
break;
}
MakeRSR m=new MakeRSR();
String name = urlImg.substring(urlImg.lastIndexOf("\\"),urlImg.lastIndexOf("."));
String output=name.substring(1); //?
String urlOutput=urlFrames+"\\finalRSR\\"+name+"-"+algo+"-dist"+dist+"-n"+numI+"-N"+spra+".png";
m.RSR(urlImg,urlOutput,dist,numI,spra);
}
}

问题是这个类将被调用多次,每次它都会覆盖变量的先前值:如果我在第一个检查点检查它们,它们是不同的(可能是因为必须进行第二次获取),但在第二个检查点,它们是相同的。我怎样才能让他们保持不同?

最佳答案

如果这些变量是由构造函数设置的,则它们不应该是静态的。它们应该是实例变量,因此类的每个实例都可以有不同的值。

public class WorkerRSR extends SwingWorker<String, Void>{
private String urlFrames;
private String urlImg;
private int bool;
private int dist;
private int numI;
private int spra;
private boolean isCoda;
private int numCoda;
private String algo;

public WorkerRSR(String urlImg, int dist, int numI, int spra, String algo, String urlFrames, boolean isCoda, int numCoda) {
this.urlImg=urlImg;
this.dist=dist;
this.numI=numI;
this.spra=spra;
this.algo=algo;
this.urlFrames=urlFrames;
this.isCoda = isCoda;
this.numCoda = numCoda;

//FIRST CHECK POINT

}

...
}

您还应该将所有这些变量更改为私有(private)。如果应从类外部访问它们,则应通过 getter 方法访问它们。

关于Java变量覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32455697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com