Java变量覆盖

标签 java overwrite

我有一个带有 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/

相关文章:

c - 在 C 中重置 char[] 的最佳方法是什么?

javascript - 我需要覆盖一个运行时函数js

java - 如何终止检测新文件的连续线程

java - 为什么HashMap的运算复杂度没有考虑hash函数的复杂度?

java - Tomcat 404 – 未找到 : when Implementing REST API

c - 在c中读取字符串时内存覆盖

java - 在 hibernate 中覆盖CreationTimestamp/UpdateTimestamp?

java - 自定义基本路径 Play Framework

php - 在 codeigniter 中上传图像时覆盖以前的文件