更新由匿名类方法引用的原始实例变量值为什么不会更新其方法中的值?

huangapple go评论68阅读模式
英文:

Why updating a primitive instance variable value referring by the method of an anonymous class does not update the value in the method of it?

问题

这里是我在维护一个旧应用程序时遇到的一个问题,以下是用于演示的代码:

首先,我创建了一个类,其中包含一个字段innerComp,其值为匿名类的实例,以及一个字段adjustNum,该字段在innerComp的方法中使用:

public class InnerComparator {
    private int adjustNum = 0;

    public InnerComparator() {

    }

    public InnerComparator(int adjustNum) {
        this.adjustNum = adjustNum;
    }

    public void setAdjustNum(int adjustNum) {
        this.adjustNum = adjustNum;
    }

    private Comparator innerComp = new Comparator() {
        @Override
        public int compare(Object o1, Object o2) {
            System.out.println(adjustNum);
            return adjustNum;
        }
    };

    public Comparator getInnerComp() {
        return innerComp;
    }

    public void sort(List list) {
        Collections.sort(list, innerComp);
    }
}

以下是我如何使用它的演示:

public class CheckInnerClassUpdate {
    public static void main(String[] args) {
        InnerComparator comparator = new InnerComparator();
        List<String> list = Arrays.asList(new String[]{"1", "2", "3"});
        comparator.sort(list);
        comparator.setAdjustNum(1);
        comparator.sort(list);
        InnerComparator comparator2 = new InnerComparator(1);
        comparator2.sort(list);
    }
}

结果是:

0
0
1
1
1
1

(过时部分)
结果是:

0
0
0
0
0
0

我想请教一下产生这个结果的机制。结果似乎表明,传递给匿名类实例的adjustNum引用的是adjustNum的初始值。在示例代码中,即使我通过构造函数传递了值,传递给innerComp的值仍然是0。我不知道的是缺少了一部分知识,希望有人可以帮助。谢谢。

(已更新部分)
如答案中所述,拼写错误导致了混淆。我已经修复了代码并将原始代码注释掉。

英文:

Here is a problem I face when maintaining and old application, below are the code for demonstration:
First, I created a class with field innerComp valued by instance of an anonymous class and a field adjustNum which is used in a method of innerComp:

public class InnerComparator {
private int adjustNum = 0;

public InnerComparator() {

}

public InnerComparator(int adjustNum) {
    this.adjustNum = adjustNum;
}

public void setAdjustNum(int adjustNum) {
  //        adjustNum = adjustNum;
      this.adjustNum = adjustNum;
}
private Comparator innerComp = new Comparator() {
    @Override
    public int compare(Object o1, Object o2) {
        System.out.println(adjustNum);
        return adjustNum;
    }
};

public Comparator getInnerComp() {
    return innerComp;
}

public void sort(List list) {
    Collections.sort(list, innerComp);
}

}

Here demonstrate how I use it:

public class CheckInnerClassUpdate {
public static void main(String[] args) {
    InnerComparator comparator = new InnerComparator();
    List&lt;String&gt; list = Arrays.asList(new String[]{&quot;1&quot;, &quot;2&quot;, &quot;3&quot;});
    comparator.sort(list);
    comparator.setAdjustNum(1);
    comparator.sort(list);
    InnerComparator comparator2 = new InnerComparator(1);
    // comparator.sort(list);
    comparator2.sort(list);
}

}

The result is:
0
0
1
1
1
1

(Obsolete)
The result is:
0
0
0
0
0
0
I would like to ask for the mechanism that generate the result. The result seems show that the adjustNum pass to the instance of the anonymous class is referring to the init value of the adjustNum. In the sample code, even if I pass the value via the constructor, the value pass to the innerComp is still 0. There is a missing piece of knowledge that I don't know and hopefully someone could help. Thank you.

(Updated)
As mentioned answer, typo cause the confusion. I have fix the code and
comment out the original code.

答案1

得分: 1

我发现你的代码中至少有一个错误:

public void setAdjustNum(int adjustNum) {
    adjustNum = adjustNum;
}

这个方法没有任何作用,因为它将作为参数传递给函数的 adjustNum 重置为自身。如果你需要设置类的 adjustNum 字段,你的代码必须更新为:

public void setAdjustNum(int adjustNum) {
    this.adjustNum = adjustNum;
}

因此,this.adjustNum 引用实例的属性,而 adjustNum(不带 this)引用传递给函数的参数。在这种情况下,通过调用 setAdjustNum 方法真正设置了 adjustNum

英文:

I found at least an error in your code:

public void setAdjustNum(int adjustNum) {
    adjustNum = adjustNum;
}

This method does nothing because it resets adjustNum passed as parameter to the function. If you need to set the adjustNum field of the class your code must be updated to

public void setAdjustNum(int adjustNum) {
    this.adjustNum = adjustNum;
}

So the this.adjustNum refers to the property of the instance, while adjustNum (without this) refers to the parameter passed to the function. In this case you really set the adjustNum calling setAdjustNum method

huangapple
  • 本文由 发表于 2023年6月15日 18:19:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76481523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定