大家好有人可以解釋爲什麼在這個代碼的最後一行是合法的訪問私有字段在Java類:從另一個類
public class HashCodeTest {
private String value = null;
HashCodeTest(String value) {
this.value = value;
}
public static void main(String[] args) {
Map<HashCodeTest, String> aMap = new HashMap<HashCodeTest, String>();
aMap.put(new HashCodeTest("test"), "test");
aMap.put(new HashCodeTest("test"), "test");
System.out.println(aMap.size());
}
@Override
public int hashCode() {
int result = 17;
return 31 * result + value.hashCode();
}
public boolean equals(HashCodeTest test) {
if (this == test) {
return true;
}
if (!(test instanceof HashCodeTest)) {
return false;
}
return test.value.equals(value);
}
}
在最後一行就有機會獲得測試類的私有字段,但是這是非法。
感謝, 馬克西姆