gpt4 book ai didi

Java的私有(private)字段访问

转载 作者:行者123 更新时间:2023-11-29 05:25:20 25 4
gpt4 key购买 nike

<分区>

我很难理解为什么我可以访问 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) 方法?

非常感谢您!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com