子类与父类变量与方法的测试

public class Base {

    private int i = 2;

    public void display() {
        system.out.println(i);
    }

    public Base() {
        display();
    }

}
public class Derived extends Base {

    private int i = 22;

    public void display() {
        System.out.println(i);
    }

    public Derived() {
        i = 222;
    }

    public static void main(String[] args) {
        Derived d = new Derived();
    }

}
输出结果为0,子类与父类的变量都存在,父类构造方法,调用子类的display()方法

赞 (0) 评论 分享 ()