class Father{
int b =100;
public Father() {
method();
}
public void method() {
System.out.println("Father_method()="+b);
}
}
class Son extends Father{
int b = 10;
public Son() {
b = 10000;
}
public void method() {
System.out.println("Son_method()="+b);
}
}
public class ExtendsDemo {
public static void main(String[] args) {
Father c1 = new Son();
System.out.println(c1.b);
Son c2 = new Son();
System.out.println(c2.b);
}
}
运行结果:
Son_method()=0
100
Son_method()=0
10000
程序的执行顺序可以参考这篇文章: