class TestPrivate2 {
private int n;
}
public class TestPrivate {
private int n;
public void accessOtherPrivate(TestPrivate other) {
other.n = 10;// can use other class's private field,why??????????
System.out.println(other.n);
}
public void accessOtherPrivate(TestPrivate2 other) {
// other.n = 10;//can not access,i konw
// System.out.println(other.n);//
}
public static void main(String[] args) {
new TestPrivate().accessOtherPrivate(new TestPrivate());
}
}
看看TestPrivate的方法:accessOtherPrivate。为什么可以使用其他类的私有字段,为什么?