不适用具有继承的构造函数



我是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 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类适用代码:

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;
    }

驱动程序尝试以下操作并收到错误:

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);

您是否意识到Type[]意味着" Type的数组"?

根据这两个构造函数声明:

public Student1(String [] theName, Date [] theDate, int [] theSocial, double [] theWageRate, double [] theHours)
public Staff1(String [] theName, Date [] theDate, int [] theSocial, double [] theWageRate, double [] theHours, double [] theSalary)

所有参数必须为数组。
但是从错误消息中可以清楚地看出,您正在向构造函数传递普通值。

从构造函数声明中删除所有的[],你应该没事。

或者,如果实参实际上都是数组,则必须这样调用构造函数:

Student1 st = new Student1(new String[]{"Jack"}, new Date[]{new Date("May", 8, 1990)}, new int[]{1}, new double[]{7.50}, new double[]{7.00});
myPerson[1] = new Staff1(new String[]{"Will"}, new Date[]{new Date("July", 10, 1998)}, new int[]{2}, new double[]{7.00}, new double[]{8.00}, new double[]{3900.00});

学生的构造函数:

public Student1(String [] theName, Date [] theDate, int [] theSocial, double [] theWageRate, double [] theHours)

接受数组作为参数。但是,当您初始化学生对象时:

new Student1("Jack", new Date("May", 8, 1990), 00000001, 7.50, 7.00);

你没有使用数组,只是简单的值。您要么需要修改构造函数以接受单个值,要么需要使用对象数组创建student。

构造函数接受数组作为参数。您需要将其更改为:

public Student1(String theName, Date theDate, int theSocial, double theWageRate, double theHours)

要了解有关数组的更多信息,请查看https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

最新更新