子类在用super调用父类中构造方法时,显示没有找到符合条件的构造方法,形式参数列表实际参数列表长度不和。
代码如下:
class People{
protected String id;
protected String name;
public People(String id , String name){
this.id=id;
this.name=name;
}
public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void say(){
System.out.println("I'm a student. My name is " + this.name + ".");
}
}
class Student extends People{
protected String sid;
protected int score;
public Student() {
name = "Pintia Student";
}
public Student(String id, String name, String sid, int score) {
super(id, name);
this.sid = sid;
this.score = score;
}
public void say() {
System.out.println("I'm a student. My name is " + this.name + ".");
}
}
public class Main {
public static void main(String[] args) {
Student zs = new Student();
zs.setId("370211X");
zs.setName("Zhang San");
zs.say();
System.out.println(zs.getId() + " , " + zs.getName());
Student ls = new Student("330106","Li Si","20183001007",98);
ls.say();
System.out.println(ls.getId() + " : " + ls.getName());
People ww = new Student();
ww.setName("Wang Wu");
ww.say();
People zl = new People("370202", "Zhao Liu");
zl.say();
}
}
在百度后发现,我相比其他人的代码我少写了一个无参的People类的构造方法
public People(){
}
修改后结果如下: