我是Java新手。最近我一直在研究构造函数,继承,数组和抽象类。我试图将所有这些概念结合到代码中以供练习。继承构造方法不适用
我有一个名为Person3的抽象基类,一个名为Student1的子类和一个名为Staff1的子类。我也有一个驱动程序。
我在编译时收到有关Student1和Staff1构造函数的错误,并希望得到一些解释,因为我只是没有看到问题。
有人可以请详细解释为什么发生这些错误?谢谢!
错误:
no suitable constructor found for Student1(String,Date,int,double,double)
Student1 st = new Student1("Jack", new Date("May", 8, 1990), 00000001, 7.50, 7.00);
^
constructor Student1.Student1(Student1) is not applicable
(actual and formal argument lists differ in length)
constructor Student1.Student1(String[],Date[],int[],double[],double[]) is not applicable
(actual argument String cannot be converted to String[] by method invocation conversion)
constructor Student1.Student1() is not applicable
(actual and formal argument lists differ in length)
no suitable constructor found for Staff1(String,Date,int,double,double,double)
myPerson[1] = new Staff1("Will", new Date("July", 10, 1998), 00000002, 7.00, 8.00, 3900.00);
^
constructor Staff1.Staff1(Staff1) is not applicable
(actual and formal argument lists differ in length)
constructor Staff1.Staff1(String[],Date[],int[],double[],double[],double[]) is not applicable
(actual argument String cannot be converted to String[] by method invocation conversion)
constructor Staff1.Staff1() is not applicable
(actual and formal argument lists differ in length)
为Student1类适用代码:
public class Staff1 extends Person3
{
private double [] salary;
public Staff1()
{
super();
for(int i = 0; i < salary.length; i++){
salary[i]=0.00;
}
}
public Staff1(String [] theName, Date [] theDate, int [] theSocial, double [] theWageRate, double [] theHours, double [] theSalary){
super(theName, theDate, theSocial);
if (theSalary != null)
salary = theSalary;
else
{
System.out.println("Fatal Error: Negative salary.");
System.exit(0);
}
}
public Staff1(Staff1 originalObject){
super((Person3)originalObject);
salary = originalObject.salary;
}
驱动程序尝试下面并接收错误::
public class Student1 extends Person3
{
private double [] wageRate;
private double [] hours;
public Student1(){
super();
for(int i = 0; i < wageRate.length; i++) {
wageRate[i] = 0.00;
}
for(int i = 0; i < hours.length; i++){
hours[i] = 0.00;
}
}
public Student1(String [] theName, Date [] theDate, int [] theSocial, double [] theWageRate, double [] theHours){
super(theName, theDate, theSocial);
if ((theWageRate != null) && (theHours != null)){
wageRate = theWageRate;
hours = theHours;
}
else{
System.out.println("Fatal Error: creating an illegal hourly employee.");
System.exit(0);
}
}
public Student1(Student1 originalObject)
{
super(originalObject);
wageRate = originalObject.wageRate;
hours = originalObject.hours;
}
为STAFF1类适用代码
Student1 st= new Student1("Jack", new Date("May", 8, 1990), 00000001, 7.50, 7.00);
myPerson[1] = new Staff1("Will", new Date("July", 10, 1998), 00000002, 7.00, 8.00, 3900.00);
与此签名的构造方法:'公共Student1(字符串[],日期[],INT [],双[],double [])'与具有此签名的构造函数不同:'public Student1(String,Date,int,double,double)' – khelwood