实现继承覆盖toString方法

时间:2012-09-24 13:54:25

标签: java

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

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

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

 
error: constructor Student in class Student cannot be applied to given types;
error: no suitable constructor found for Employee(String,String,String,String)
error: constructor Faculty in class Faculty cannot be applied to given types;
error: no suitable constructor found for 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 个答案:

答案 0 :(得分:4)

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

  

错误:构造函数类Student中的Student不能应用于给定   类型;错误:找不到合适的构造函数   雇员(字符串,字符串,字符串,字符串)

这意味着在你拥有这个代码之前你不会得到编译代码:

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;

}

<强>更新

你的问题是学生只有这个构造函数:

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

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

答案 1 :(得分:2)

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

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

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

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

答案 2 :(得分:2)

Person的构造函数需要String作为第三个参数,但是您尝试将int phone传递给子类中的超级构造函数。这不会起作用,因为它的类型错误。

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

  1. 添加或减去电话号码没有意义。
  2. 整数不允许使用前导零,这些零用于某些区域代码 国家。
  3. 整数不能大于2147483648.当你 试着存储很长的电话号码,这看似随心所欲 限制将导致非常意外的错误。

答案 3 :(得分: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");
    }
}