继承中的this

父类:

public class Parent  {
    //看这里的this指的是什么
    public Parent() {
        this.say();
    }
    
    protected void say() {
        System.out.println("father");
    }
}

子类:

public class Sun extends Parent{
    public Sun() {
    }

    @Override
    protected void say() {
        System.out.println("sun");
    }
}

测试实例化子类时候,父类中的this指的是什么:

public class TestExtends {
    public static void main(String[] args) {
        Sun sun = new Sun();
    }
}

debug:

 

 

 这里的this是当前对象,也就是sun,所以执行sun中的say方法。

结果:

 

posted @ 2020-11-17 23:31  圣金巫灵  阅读(87)  评论(0)    收藏  举报