super、this

public class OverreadDemo {
/* * 重写面试题 * *1、 super、this分别是父类和本类的引用, *2、 可调用构造函数super(); * 可传递普通函数;可调用成员变量; */
    public static void main(String[] args) {
        new B();
    }
}
class A {
    public A() {
        System.out.println("A构造");
    }
    public void show() {
        System.out.println("A");
    }
}
class B extends A{
    int x = 24;
    public B() {
        super();//默认父类的构造方法
        this.show();
        super.show();//调用普通方法
    }
    public void show() {
        System.out.println("B");
        System.out.println(this.x);
    }
}
全部评论

相关推荐

11-20 13:39
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务