正确代码解释如下:
class My {
public static void main(String[] args) {
Father father = new Father("tom");
Son son = new Son();
father.show();
son.print();
}
}
class Father {
String name;
Father() {}
Father(String name) {
this.name = name;
System.out.println("father 构造");
}
public void show() {
System.out.println("father");
}
}
class Son extends Father {
String name = "tom1";
Son(){
//因为这里有一个默认的super()调用父类的无参构造函数,而你的父类没有,所以报错
//或手动写上调用父类有参构造方法 super("..");错误就会消失
System.out.println("son 构造");
}
public void print() {
System.out.println("son");
}
}