我只注意到覆蓋方法的行爲與覆蓋字段的行爲不同。 考慮下面的代碼片斷:覆蓋方法和字段的不同行爲
public class Bar {
int v =1;
public void printAll(){
System.out.println(v);
printV();
}
public void printV(){
System.out.println("v is " + v);
}
}
public class Foo extends Bar {
int v = 4;
public static void main(String[] args) {
Foo foo = new Foo();
foo.printAll();
}
public void printV() {
System.out.println("The value v is " + v);
}
}
它導致輸出:
值v爲4
因此看來,在欄的方法printV由foo.printV重寫而欄v不會被覆蓋。有沒有人知道這種差異的原因?
謝謝。
有沒有這樣的事情在java中重寫字段。輸出是正確的。 – 2012-08-15 14:23:06