首页 > 技术文章 > java实例变量及方法调用顺序

yasepix 2016-08-12 11:47 原文

public class Base {
    private String name="base";

    public Base(){
        sayHello();
    }
    void sayHello() {
        System.out.println("Base hello " + name);
    }
}

class Derived extends Base {
    private String name="Derived";

    public Derived(){
        sayHello();
    }

    void sayHello() {
        System.out.println("Derived hello " + name);
    }
}

public class Main {
    public static void main(String[] args) {
        Base b = new Derived();
    }
}

 

Derived hello null
Derived hello Derived

 

推荐阅读