0

我创建了一个(人、学生、员工、教职员工)类。Person 必须是 Student 和 Employee 的子类。Employee有两个子类Faculty和Staff。我已经完成了所有的编码,它们工作正常,除了我的驱动程序类 TestPerson 程序它给出编译错误

注意:一个创建 Person、Student、Employee、Faculty、Staff 并调用其 toString 方法的测试程序。

驱动类TestPerson.java的错误如下:-

 
错误:Student 类中的构造函数 Student 不能应用于给定类型;
错误:没有为 Employee(String,String,String,String) 找到合适的构造函数
错误:类 Faculty 中的构造函数 Faculty 不能应用于给定类型;
错误:没有为 Staff(String,String,String,String) 找到合适的构造函数”

**我只是提供驱动类的代码。如果您需要我的其他类的其他编码,请在评论中说明,我会立即发布。

请在下面查看我的编码:-

public class TestPerson {

    public static void main(String[] args) {
        Person person = new Person("John Doe", "123 Somewhere", "415-555-1212", "johndoe@somewhere.com");
        Person student = new Student("Mary Jane", "555 School Street", "650-555-1212", "mj@abc.com", "junior");
        Person employee = new Employee("Tom Jones", "777 B Street", "40-88-889-999", "tj@xyz.com");
        Person faculty = new Faculty("Jill Johnson", "999 Park Ave", "92-52-22-3-333", "jj@abcxyz.com");
        Person staff = new Staff("Jack Box", "21 Jump Street", "707-21-2112", "jib@jack.com");

        System.out.println(person.toString() + "\n");
        System.out.println(student.toString() + "\n");
        System.out.println(employee.toString() + "\n");
        System.out.println(faculty.toString() + "\n");
        System.out.println(staff.toString() + "\n");
        }
}

//人物类

public class Person {

    private String name;
    private String address;
    private String phone_number;
    private String email_address;

    public Person() {
    }

    public Person(String newName, String newAddress, String newPhone_number, String newEmail){
        name = newName;
        address = newAddress;
        phone_number = newPhone_number;
        email_address = newEmail;
    }

    public void setName(String newName){
        name = newName;
    }

    public String getName(){
        return name;
    }

    public void setAddress(String newAddress){
        address = newAddress;
    }

    public String getAddress(){
        return address;
    }

    public void setPhone(String newPhone_number){
        phone_number = newPhone_number;
    }

    public String getPhone(){
        return phone_number;
    }

    public void setEmail(String newEmail){
        email_address = newEmail;
    }

    public String getEmail(){
        return email_address;
    }

    public String toString(){
        return "Name :"+getName();
    }

}

//学生班级

public class Student extends Person {

    public final String class_status;

    public Student(String name, String address, int phone, String email, String classStatus) {
    super(name, address, phone, email);
    class_status = classStatus;
    }
    public String toString(){
        return "Student Status: " + super.getName();
    }

}

//员工类

import java.util.Date;
public class Employee extends Person{

    private String office;
    private double salary;
    private Date hire;

    public Employee() {
    }

    public Employee(String name, String address, int phone, String email){
        super(name, address, phone, email);
    }

    public Employee(String office, double salary, Date hire){
        this.office = office;
        this.salary = salary;
        this.hire = hire;
    }

    public void setOffice(String office){
        this.office = office;
    }

    public String getOffice(){
        return this.office;
    }

    public void setSalary(double salary){
        this.salary = salary;
    }

    public double getSalary(){
        return this.salary;
    }

    public void setHire(Date hire){
        this.hire = hire;
    }

    public Date getHire(){
        return this.hire;
    }

    public String toString(){
        return "Office " + super.getName();
    }
}

//教师班

public class Faculty extends Employee {
    private String officeHours;
    private int rank;

    public Faculty(String name, String address, int phone, String email) {
    super(name, address, phone, email);
    }

    public String toString(){
        return "Office " + super.getOffice();
    }
}

//员工类

public class Staff extends Employee {
    private String title;

    public Staff(String name, String address, int phone, String email) {
    super(name, address, phone, email);
    }

    public Staff(String title){
        this.title = title;
    }

    public void setTitle(String title){
        this.title = title;
    }
    public String getTitle(){
        return this.title;
    }

    public String toString(){
        return "Title :" + super.getName();
    }
}
4

4 回答 4

4

您收到这些错误的原因是构造函数不存在。

错误:Student 类中的构造函数 Student 不能应用于给定类型;错误:没有为 Employee(String,String,String,String) 找到合适的构造函数

这意味着在您拥有以下代码之前,您将无法编译代码:

Student(String name, String addr, String phone, String email) {
    ....
}

假设您已经在构造函数中设置了属性,toString 将如下所示:

public String toString() {
    return this.name + "\n" + this.addr + "\n" + this.phone + "\n" + this.email;

}

更新

您的问题是 Student 只有这个构造函数:

public Student(String name, String address, int phone, String email, String classStatus)

Student 需要一个只接受四个字符串作为参数的构造函数。或者,您可以使所有内容都采用您指定的五个参数。

于 2012-09-24T14:00:57.170 回答
2

它可能与问题本身无关,但我认为设计可以这样改进:

  • 用角色名称定义抽象类角色
  • 定义类 Student、Employee、Staff 继承 Role
  • 为所有人员类型、名称等定义具有公共属性的 Person 类,并在内部具有 Role 类型的属性

然后在 Person 和所有 Role 实现中定义 toString

通过这种方式,您将能够独立于角色扩展或修改 Persons,这使得设计更加灵活

于 2012-09-24T14:04:32.350 回答
2

Person 的构造函数需要一个 String 作为第三个参数,但您正试图将其传递int phone给子类中的超级构造函数。那是行不通的,因为它是错误的类型。

顺便说一句:你应该总是用字符串而不是整数来表示电话号码。

  1. 添加或减去电话号码是没有意义的。
  2. 整数不允许使用前导零,在某些国家/地区用于区号。
  3. 整数不能大于 2147483648。当你尝试存储很长的电话号码时,这个看似随意的限制会导致非常意想不到的错误。
于 2012-09-24T14:04:40.083 回答
1

首先在所有类中将电话号码数据类型设置为整数..

主要功能将是:

public class TestPerson {
    public static void main(String[] args) {
        Person person = new Person("John Doe", "123 Somewhere", "415-555-1212",
                                   "johndoe@somewhere.com");
        Person student = new Student("Mary Jane", "555 School Street", 
                                     650-555-1212, "mj@abc.com", "junior");
        Person employee = new Employee("Tom Jones", "777 B Street", 
                                       40-88-889-999,  "tj@xyz.com");
        Person faculty = new Faculty("Jill Johnson", "999 Park Ave",
                                     92-52-22-3-333, "jj@abcxyz.com");
        Person staff = new Staff("Jack Box", "21 Jump Street", 707-21-2112, 
                                 "jib@jack.com");

        System.out.println(person.toString() + "\n");
        System.out.println(student.toString() + "\n");
        System.out.println(employee.toString() + "\n");
        System.out.println(faculty.toString() + "\n");
        System.out.println(staff.toString() + "\n");
    }
}
于 2013-11-25T19:06:44.137 回答