1.結構
class Test{
private int a=1;
protected int b=2;
public int c=3;
public int getA() {
return a;
}
}
public class Test2 extends Test{
public int c;
public static void main(String[] args) {
Test2 test = new Test2();
System.out.println(test.a);
System.out.println(test.getA());;
System.out.print(test.b);
}
}
2.結果
1)會報錯
Exception in thread “main” java.lang.Error: Unresolved compilation problem:
The field Test.a is not visible
子類對象並不能直接調用父類的私有變量
2)但是能夠繼承父類的私有變量
3)而且無論父類中的成員變量是否被子類覆蓋,子類都會擁有父類中的這些成員變量。