请解释一下this和super的区别?

****面试题请解释一下this和super的区别?
在这里插入图片描述

class A {
    public void print() {
        System.out.println("Hello World .") ;
    }
}
class B extends A {
    public void print() {
       super.print() ;
        System.out.println("世界,你好!") ;
    }
}
public class Test {
    public static void main(String args[]) {
        B b = new B() ;
        b.print() ;	// 方法从父类继承而来
    }
}

运行结果
切记:
· this.方法():先从本类查找是否存在指定的方法,如果没有找到,则调用父类操作;
· super.方法():直接由子类调用父类之中的指定方法,不再找子类。

猜你喜欢

转载自blog.csdn.net/weixin_43258908/article/details/88546934