最近我偶然發現了Java測試,發現了一個奇怪的行爲。Java繼承基類使用派生類方法
class MainOne {
private int i = 5;
public MainOne() {
System.out.println("MainOne says that I is " + getI());
}
public int getI() {
System.out.println("MainOne getI was used");
return i;
}
}
public class TheTest extends MainOne {
private static int i = 10;
public TheTest() {
System.out.println("TheTest says that I is " + super.getI());
}
public int getI() {
System.out.println("TheTest getI was used");
return i;
}
public static void main(String[] args) {
TheTest test = new TheTest();
}
}
結果爲:
TheTest格提使用
銘萬說,我是10.
銘萬格提使用
TheTest說我是5
問題是,發生了什麼事? Base類如何使用它的後代方法?
你是什麼意思?它們在構造函數中被調用。 – Rofgar