<分区>
我很难理解为什么我可以访问 this. 上下文之外的私有(private)字段? 为了澄清,我添加了一个小的 MyClass 示例:
public class MyClass {
private int myPrivateInt;
public MyClass(int myPrivateInt) {
this.myPrivateInt = myPrivateInt;
}
public boolean equals(Object obj) {
// if it's not an instance of MyClass it's obviously not equal
if (!(obj instanceof MyClass)) return false;
MyClass myClass = (MyClass) obj;
// here comes the part I don't quite understand fully:
// why can I access a private field outside of the "this." context?
return this.myPrivateInt == myClass.myPrivateInt;
}
}
这是故意的语言选择还是根本无法区分 this. 上下文和(或多或少)传递给 equals(对象 obj) 方法?
非常感谢您!