小编Ama*_*jan的帖子

当没有定义父类时 super() 方法不会给出错误

class child
{
    child()
    {
        super();
        System.out.println("Hello");
    }
    public static void main(String arg[])
    {
        child obj=new child();
    }
}
Run Code Online (Sandbox Code Playgroud)

在此代码中,当我创建 child 类的对象时,将调用子构造函数。但为什么它没有给出错误,因为没有父类。super()在这里做什么?super() 关键字调用了谁的构造函数?

java super

2
推荐指数
1
解决办法
1824
查看次数

这个代码中返回的是什么?

在这段代码中

class Parent {

    void show() {
        System.out.print("parent");
    }

    Parent a() {
        return this;
    }

}

class Child extends Parent {

    void show() {
        System.out.print("child");
    }

    public static void main(String arg[]) {
        Parent a = new Child();
        Parent b = a.a();
        b.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

怎么return this;办?b.show()正在调用子方法show.那么this返回引用它的子类吗?如果没有,那么如何show()调用孩子的方法?

java

1
推荐指数
1
解决办法
1920
查看次数

标签 统计

java ×2

super ×1