public class Main {
static class Father {
private int money = 1;
public Father() {
this.money = 2;
this.showMoney();
}
public void showMoney() {
System.out.println("I am father, I have $" + this.money + ".");
}
}
static class Son extends Father {
private int money = 3;
public Son() {
this.money = 4;
this.showMoney();
}
@Override
public void showMoney() {
System.out.println("I am son, I have $" + this.money + ".");
}
}
public static void main(String[] args) {
final Son son = new Son();
System.out.println("I am son, I have $" + son.money + ".");
}
}
输出结果
I am son, I have $0.
I am son, I have $4.
I am son, I have $4.
后面两行我都能理解,但是第一次输出时为什么 money 字段是 0 ?
谢谢