【问题标题】:Java variables overwritingJava 变量覆盖
【发布时间】:2015-12-04 00:21:03
【问题描述】:

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

工人阶级:

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);
    }
}

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

【问题讨论】:

    标签: java overwrite


    【解决方案1】:

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

    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 方法访问它们。

    【讨论】:

    • 访问修饰符也是public
    • 首先,感谢两位! :) 正如你所看到的,我是一个新手,你真的帮了我很多!我会带走静电。我不需要从外部访问变量(此类接收这些参数,然后执行以图像保存结束的操作,因此没有获取)。 @斯蒂芬:你是什么意思?我应该把同样的东西改成公开吗?
    • @pedro 他可能的意思是你应该把所有的变量都改成私有的。
    • @pedro 我的意思是数据封装是一种很好的做法,关键是属性只能在类内部修改.. public 修饰符使它们容易受到外部修改
    • @pedro 你的go 方法是静态的,所以它不能访问你的类的实例成员。我不确定您打算如何使用该方法。也许它不应该是静态的。如果它应该是静态的,则需要 WorkerRSR 类的实例才能访问它的实例成员。
    猜你喜欢
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 2013-10-27
    相关资源
    最近更新 更多