我的子类构造函数中的构造函数错误

时间:2014-06-27 13:02:12

标签: java constructor subclass

这是作业:

使用以下指南设计并实现一个表示Person以及3个子类的类:

a。创建一个名为Person的类及其名为Employee,Student,Retired的三个子类。

b。Person有以下数据字段:name,year_of_birth,isStudying和isEmployed。它还具有设置和获取每个字段的值的方法,以及计算当前年龄和显示Person状态的方法。将isStudying和isEmployed字段设置为false的构造函数也包含在Person类中。如果您愿意,欢迎您添加其他数据字段和方法。

1.最后,创建一个模拟使用Person类的Java测试类。在您的测试类中,您至少应该:a)构造4个Person实例,b)打印实例的名称c)根据其年龄,isStudying和isEmployed属性的值打印实例的状态。

public class Person2 {//begin class
    //declare variables
    String name;
    int year_of_birth;
    boolean isStudying;
    boolean isEmployed;
    int age;

public Person2(String name, int year_of_birth, boolean isEmployed, boolean isStudying, int age){//begin constructor
    this.name = name;
    this.year_of_birth = year_of_birth;
    this.isEmployed = false;
    this.isStudying = false;
    this.age = age;
}//end constructor

public int getYear(){//get year method
        return year_of_birth;
}//end method

public String getName(){//get name method
        return name;
}//end method

public boolean getEmployed(){//get employed method
        return isEmployed;
}//end method

public boolean getStudying(){//get employed method
        return isStudying;
    }//end method

public int getAge(){//get year method
        age = 2014 - year_of_birth;
    return age;
}//end method

public void setName(String name){//set name method
        this.name = name;
}//end method

public void setYear (int year){//set year method
        this.year_of_birth = year;
}//end method

public void setEmployed(boolean employed){//set employed method
        this.isEmployed = employed;
}//end method

public void setAge (int age){//set year method
        this.age = age;
}//end method

public static void main(String[] args) {
        // TODO code application logic here
}

}

class Student extends Person2 {//begin class

    public Student(String name, int year_of_birth, boolean isEmployed, boolean isStudying, int age){//begin constructor
        this.name = name;
        this.year_of_birth = year_of_birth;
        this.isEmployed = isEmployed;
        this.isStudying = isStudying;
        this.age = age;
}//end constructor)

    @Override
    public int getYear(){//get year method
        return year_of_birth;
    }//end method

    @Override
    public String getName(){//get name method
        return name;
    }//end method

    @Override
    public boolean getEmployed(){//get employed method
        return isEmployed = false;
    }//end method

    @Override
    public boolean getStudying(){//get employed method
        return isStudying = true;
    }//end method

    @Override
    public int getAge(){//get year method
    age = 2014 - year_of_birth;
    if (age > 30){
        System.out.println("Person is not a student");
    }
    return age;
}//end method

}

这段代码显然不完整我对此构造函数错误感到困惑。它说“实际和正式的论点长度不同”。

2 个答案:

答案 0 :(得分:4)

您的问题是您没有调用超级构造函数Person2Student中的构造函数正在尝试调用Person2的默认构造函数(没有参数),该构造函数不存在。

Person2中只有一个构造函数,应该从Student的构造函数中调用该构造函数:

public Student(String name, int year_of_birth, boolean isEmployed, boolean isStudying, int age) {
    super(name, year_of_birth, isEmployed, isStudying, age);
}

这也意味着您不需要重复所有初始化代码两次。

您也不应该重复StudentPerson2中的所有方法。如果你把它们拿出来,Student无论如何都会继承它们。这就是首先扩展课程的重点。如果您不想要继承的行为,而只想要一些Student特定的行为,那么您应该只覆盖这样的方法。

答案 1 :(得分:0)

扩展任何功能时,您应尝试使用该功能...
您的class Student extends Person2但它是否使用Person2中的任何内容?

为什么要覆盖所有方法并在子类中提供相同的功能???