了解 return 没有方法 return 的实例变量
Understanding for returning an instance variable without a method to return it
所以我只是想寻求澄清。我有一个 equals 方法,它能够通过 cv.ch
传递对象的 return 实例变量,而没有 return 它的方法。怎么会这样?
public static class Test {
private int v;
private char ch;
public Test(int v, char ch) {
this.v= v;
this.ch= ch;
}
public boolean equals(Object o) {
if ( this == o ) return true;
if ( o == null || this.getClass() != o.getClass() )
return false;
Test cv = (Test) o;
if ( this.v == cv.v && this.ch == cv.ch)
return true;
return false;
}
}
编辑:我改写了我的问题,以便更好地理解
私有成员变量可供拥有它们的 class Test 访问。 Test 中的任何代码都可以访问这些字段,无论它是通过 "this" 还是其他变量。
具体来说 JLS 表示“[I]f 成员或构造函数被声明为私有,则当且仅当它发生在顶层 class 的主体内时才允许访问(§ 7.6) 包含成员或构造函数的声明。"
所以我只是想寻求澄清。我有一个 equals 方法,它能够通过 cv.ch
传递对象的 return 实例变量,而没有 return 它的方法。怎么会这样?
public static class Test {
private int v;
private char ch;
public Test(int v, char ch) {
this.v= v;
this.ch= ch;
}
public boolean equals(Object o) {
if ( this == o ) return true;
if ( o == null || this.getClass() != o.getClass() )
return false;
Test cv = (Test) o;
if ( this.v == cv.v && this.ch == cv.ch)
return true;
return false;
}
}
编辑:我改写了我的问题,以便更好地理解
私有成员变量可供拥有它们的 class Test 访问。 Test 中的任何代码都可以访问这些字段,无论它是通过 "this" 还是其他变量。
具体来说 JLS 表示“[I]f 成员或构造函数被声明为私有,则当且仅当它发生在顶层 class 的主体内时才允许访问(§ 7.6) 包含成员或构造函数的声明。"