2015-10-19 61 views
-1

我是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); 
+1

与此签名的构造方法:'公共Student1(字符串[],日期[],INT [],双[],double [])'与具有此签名的构造函数不同:'public Student1(String,Date,int,double,double)' – khelwood

回答

1

您是否知道Type[]的意思是“Array of 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}); 
+0

你好,但是变量的值是数组,我怎样才能用对象数组创建一个学生呢? –

+0

@Justin更新了我的答案 – Siguza

+0

谢谢!这正是我想要抓住的。完全理解,我最初有两个声明采用数组值的构造函数,但驱动程序实际上使用了导致错误的简单值。因此,您的更新解释说,调用构造函数需要使用数组和{}的新实例,我的理解是正确的吗? –

1

您的学生的构造函数:

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

您不使用数组,只是简单的值。您需要修改您的构造函数以获取单个值,或者您需要使用对象数组创建一个学生。

+0

Hello。您能否详细说明“用物体阵列创建学生”的含义? –

+0

@Justin你传递给'Student'构造函数的参数需要是数组。或者构造函数必须使用普通的值而不是数组。 – Siguza

+0

当然。所以..因为它听起来像你不想修改你的构造函数 - 这很好。你需要传递数组值。为了做到这一点,你可以做到以下几点:'new Student1(new String [] {“Jack”},new Date [] {(new Date(“May”,8,1990))''等等... – ltalhouarne

0

你的构造正在采取数组作为参数。您需要将其更改为:

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

要了解更多有关数组,检查出https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

+0

您好。但变量值是s不想成为数组。我怎样才能用对象数组创建一个学生呢? –

+0

如果是这样的话,那么你需要改变你传递的参数。 'Student1 st = new Student1(“Jack”,new Date(“May”,1990,19),00000001,7.50,7.00);' 注意这些是如何取单个值而不是数组。 ''“Jack”'是一个'String'对象,但构造函数需要一个'String'数组。 ''new Date(...)“'是Date对象,但构造函数正在请求Date对象的数组。等等等等。 –