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() 关键字调用了谁的构造函数?
在这段代码中
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()调用孩子的方法?