我想在Donator类中创建一个类Person的数组,但出现此错误“错误:类Person中的构造方法Person无法应用于给定类型;”
我错过任何重要的代码吗?
这是我的代码。
人.java
public class Person
{
private String Name, Address, Gender, BloodType;
private int ICNumber;
private double Height, Weight;
//constructor
public Person(String n, String add, String gen, String bt, int ic, double h, double w)
{
Name = n;
Address = add;
Gender = gen;
BloodType = bt;
ICNumber = ic;
Height = h;
Weight = w;
}
//abstract method
//abstract void printPerson();
} //close Person
Donator.java
public class Donator extends Person
{
private String donatorID;
private Person[] myDonator;
private int numberOfDonator;
//constructor
public Donator(String id, String d)
{
donatorID = id;
myDonator = new Person[2];
}
public String getDonatorID()
{
return donatorID;
}
}//close Donator
最佳答案
由于要在Person
类中扩展Donator
,因此应首先在Donator
类中调用超级构造函数。
//constructor
public Donator(String id, String d)
{
// this is the Person constructor.
super("some string", "some string", "some string", "some string", 1, 1, 1);
donatorID = id;
myDonator = new Person[2];
}
这是因为,java编译器尝试将代码放入您的构造函数中,该构造函数将调用基类的Default构造函数,因为我们没有基类的default构造函数,因此会出现编译错误。