gpt4 book ai didi

java - 实现重写 toString 方法的继承

转载 作者:行者123 更新时间:2023-12-01 06:55:46 26 4
gpt4 key购买 nike

我创建了一个(人、学生、雇员、教职员工)类。 Person 必须对 Student 和 Employee 进行子类化。Employee 有两个子类:Faculty 和 Staff。我已经完成了所有编码,它们工作正常,除了我的驱动程序类 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)"

**I am just providing the codes of the driver class. If you need my other codings of the other classes, please state in the comment, and I will immediately post it.

Please see my codings below:-

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

}

//学生类(class)

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

//师资类(class)

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

最佳答案

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

error: constructor Student in class Student cannot be applied to given types; error: no suitable constructor found for 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 需要一个仅使用四个字符串作为参数的构造函数。或者,您可以使所有内容都采用您指定的五个参数。

关于java - 实现重写 toString 方法的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12566511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com