Java变量覆盖 [英] Java variables overwriting

查看:52
本文介绍了Java变量覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于的程序,该程序从Arraylist中获取字符串,将其拆分并将其发送到Worker类.

I have a program with a for that take strings from an Arraylist, split them and sends them to a 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);
    }
}

问题在于,该类将被多次调用,并且每次覆盖该变量的先前值时:如果我在第一个Checkpoint对其进行检查,则它们是不同的(可能是因为必须进行第二次获取)),但在第二个Checkpoint上,它们是相同的.我该如何让他们保持与众不同?

The problem is that this class is going to be called multiple times and every time it overwrite the previous values of the varables: if I check them at the first Checkpoint they are different (probably beacuse the second acquisition yet has to be made), but at the second Checkpoint they are the same. How can I let them stay different?

推荐答案

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

These variables shouldn't be static if they are set by the constructor. They should be instance variables, so each instance of your class can have different values.

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

    }

    ...
}

您还应该将所有这些变量更改为私有.如果应从类外部访问它们,则应通过getter方法对其进行访问.

You should also change all those variables to be private. If they should be accessed from outside the class, they should be accessed via getter methods.

这篇关于Java变量覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆