类基础:
public class Base {
private String name = "base";
public Base() {
tellName();
printName(name);
}
public void tellName() {
System.out.println("Base tell name:" + name);
}
public void printName(String name) {
System.out.println("Base print name:" + name);
}
}
类派生:
public class Derived extends Base {
private String name = "Derived";
public Derived() {
tellName();
printName(name);
}
public void tellName() {
System.out.println("Derived tell name:" + name);
}
public void printName(String name) {
System.out.println("Derived print name:" + name);
}
public static void main(String[] args) {
Derived derived = new Derived();
}
}
结果:
Derived tell name:null
Derived print name:base
Derived tell name:Derived
Derived print name:Derived
最近,有人问我这个问题。我想了想,但没有确切答案为什么这个程序部分有这样的执行结果。希望有人能帮我分析一下这样的问题。非常感谢!^-^
我只想知道派生条件下构造函数的执行过程。