請參閱下面的程序和評論:Java保護變量可以在沒有繼承的情況下在同一個包中被訪問如何?
請告訴我如何另一個類可以訪問保護成員沒有繼承?我在最後編譯並運行了這個程序。
class Sample3
{
final protected String Var1 = "Sample 3 Final Varaible";
final private String Var2 = "Sample 3 Final Varaible";
}//class Sample3
class Sample4
{
public static void main(String args[])
{
Sample3 s3=new Sample3();
//System.out.println(s3.Var2);// Line 12 : this is not accessible as the Private member is being accessed
System.out.println(s3.Var1);//Line 13 : this access the protected member but i have not used inheritance between 2 classes Sample3 and Sample4
}//end of main
}/class Sample4
Var1是類保護成員,Var2是類私有成員。
我在Sample4中創建Sample3的對象。第12行顯然是一個錯誤,但它如何編譯第13行?