public Father(String name){
System.out.println("father name");
}
}
class Son extends Father{
public Son(){
System.out.println("Son");
}
}
public class TestExtends{
public static void main(String[] args){
new Father("dddd");
new Son();
}
}
class Father{
public Father(){
System.out.println("father name");
}
public Father(String name){
System.out.println("father name");
}
}
class Son extends Father{
public Son(){
System.out.println("Son");
}
}
public class TestExtends{
public static void main(String[] args){
new Father("dddd");
new Son(); //调用Son无参数构造方法是会先调用父类的无参数构造方法,而你的父类Father没有定义无参数构造方法,正确的代码应该在父类Father中添加无参数构造方法
}
}
没有给父类写无参数的构造函数,因为你的子类构造函数是无参数的,具体程序如下:
class Father{
public Father()
{}
public Father(String name){
System.out.println("father name");
}
}
class Son extends Father{
public Son(){
System.out.println("Son");
}
}
public class TestExtends{
public static void main(String[] args){
new Father("dddd");
new Son();
}
}