加的话,是什么原因,不加的话有什么影响呢。
我在这先感谢大神回答我的问题啦~
不加其实也没有影响,子类的无参或者有参构造函数都会默认调用父类的无参构造函数,老师这里可能只是强调一下Exception中默认构造函数做的工作吧。
public class TestExtends {
public static void main(String[] args) {
new Child();
System.out.println("-----------");
new Child(1,"1");
}
}
class Parent {
protected Integer id;
protected String name;
public Parent() {
System.out.println("parent no arg constructor");
}
public Parent(Integer id, String name) {
this.id = id;
this.name = name;
System.out.println("parent all args constructor");
}
}
class Child extends Parent{
public Child() {
System.out.println("child no arg constructor");
}
public Child(Integer id, String name) {
this.id = id;
this.name = name;
System.out.println("child all args constructor");
}
}打印
parent no arg constructor
child no arg constructor
-----------
parent no arg constructor
child all args constructor