public class A {
protected int b = 16;
}
public class B extends A{
private int b=20;
public static void main(String[] args)
{
B b = new B();
System.out.println(b.b);
}
}
输出为 20。引用 B 类型对象的 b 为何不能正确访问私有(private)成员?
最佳答案
您正在获取 B 类中 b 的值。B 可以访问它自己的私有(private)成员。
关于java - 为什么我可以访问b的值,即使它在子类中设置为私有(private)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12022213/