Java:子類能夠繼承父類的私有變量?

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)而且無論父類中的成員變量是否被子類覆蓋,子類都會擁有父類中的這些成員變量。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章