【问题标题】:Implementing inheritance overriding the toString method实现覆盖 toString 方法的继承
【发布时间】:2012-09-16 00:00:14
【问题描述】:

我创建了一个(Person、Student、Employee、Faculty 和 Staff)类。 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();
    }
}

【问题讨论】:

  • 我认为您需要发布这些类的构造函数。
  • 错误不在tostring。您正在调用不存在的构造函数。
  • 好的,我现在发布我的其他类编码:-
  • 您应该避免使用int 来引用电话号码。使用String,或者更好的是,使用特定的电话号码类别。

标签: java


【解决方案1】:

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

错误: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;

}

更新

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

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

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

【讨论】:

  • 是的......它奏效了。,。我太愚蠢了...我把变量 int 然后尝试将输出作为字符串...谢谢迈克...真的很喜欢它
  • 根据经验,当您看到这样的错误时,请密切注意您传递的参数。要么该方法根本不存在,要么参数以某种方式错误。
【解决方案2】:

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

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

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

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

【讨论】:

    【解决方案3】:

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

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

    1. 加减电话号码是没有意义的。
    2. 整数不允许有前导零,在某些情况下用于区号 国家。
    3. 整数不能大于 2147483648。当你 尝试存储很长的电话号码,这看似随意 限制会导致非常意想不到的错误。

    【讨论】:

    • 没有。我真的会用电话号码类型实现电话号码。您不能连接电话号码,也不能执行大写/小写操作或类似操作。如果它们是字符串等,则无法隐式验证它们的构造。
    • 好吧,当您对区号或分机进行特殊处理时,连接电话号码是有意义的。最干净的方法是创建一个 PhoneNumber 类,该类检查输入是否为有效的电话号码,并自动丢弃一些人为便于阅读而任意添加到电话号码的破折号或空格。但在大多数情况下,这将是过度设计。而且你不会使用字符串在内部存储数字吗?
    【解决方案4】:

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

    主要功能是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-20
      • 1970-01-01
      • 2016-06-04
      • 2016-01-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 2011-06-14
      相关资源
      最近更新 更多