public class Parent {
public Parent(){
System.out.print("parent()");
}
public void m(int x){
System.out.print(x);
}
}
class Child extends Parent {
public Child() {
System.out.print("child()");
}
public static void main(String arg[]){
new Child().m(5);
}
}
parent()child()5
输出5啊,子类可以调用基类的公有函数
参考GPT和自己的思路:
根据上述代码,运行程序将输出 "parent()child()5"。这是因为子类 Child 继承了父类 Parent 的无参构造方法,所以在创建 Child 对象时,先会调用父类的构造方法输出 "parent()",然后再调用子类的构造方法输出 "child()"。最后,在 main 方法中调用了 Child 对象的 m 方法并传入参数 5,所以会输出 5。
这种随便找个在线编译网站,把代码粘进去,一执行,不就有了
只要涉及的是“是什么”而不是“为什么”,都不需要问别人