爲什麼我們不能覆蓋Java中的變量,它隱藏變量我們爲什麼不能覆蓋Java中的變量
class A {
int a = 4;
public void test(){
System.out.println("Test method of A");
}
}
class B extends A {
int a = 5;
public void test(){
System.out.println("Test method of B");
}
public static void main(String s[]){
A a= new B();
System.out.println("Value of a : " a.a);
System.out.println("Method result " a.test());
}
}
輸出::
Value of a : 4
Mthod result :Test method of B
由於B的類測試方法被叫,但變量訪問從超級參考
'class B extends B' ...什麼? – Makoto
對不起其延伸的 – varsha
已經解釋[這裏] [1]和[這裏] [2] 問候 [1]:http://stackoverflow.com/questions/427756/overriding-a-超級實例變量 [2]:http://stackoverflow.com/questions/8983002/how-are-the-variables-overridden-in-java – ultraklon