Java 继承 私有成员变量 初始化顺序的问题(笔试题)

class Base{
    private String name = "base";

    Base(){
        tellName();
        printName(name);
    }
    public void tellName(){
        System.out.println("tellBase "+ name);
    }
    public void printName(String name){
        System.out.println("printBase "+ name);
    }
}
class Drived extends Base{
    private String name = "Drived";
    Drived(){
        tellName();
        printName(name);
    }
    public void tellName(){
        System.out.println("tellChild "+ name);
    }
    public void printName(String name){
        System.out.println("printChild "+ name);
    }
}
public class TestObject {
    public static void main(String args[]){
        new Drived();

    }
}
结果为:
tellChild null
printChild base
tellChild Drived
printChild Drived

Base构造函数调用2个函数,都是调用了子类中的函数,这是构造函数中的多态

Java 继承 私有成员变量 初始化顺序的问题(笔试题)_第1张图片


你可能感兴趣的:(Java 继承 私有成员变量 初始化顺序的问题(笔试题))