[英]Implementing inheritance overriding the toString method
我创建了一个(人,学生,员工,教师和职员)类。 人必须将学生和雇员分类。 雇员有两个子类:教职员工。 我已经完成了所有编码,并且它们工作正常,但我的驱动程序类 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();
}
}
出现这些错误的原因是,构造函数不存在。
错误:班级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)
学生需要一个仅包含四个字符串作为其参数的构造函数。 或者,您可以使所有内容都使用您指定的五个参数。
它可能与问题本身无关,但我认为可以像这样完善设计:
然后在Person和所有Role实现中定义toString
这样,您将能够独立于角色来扩展或修改人员,从而使设计更加灵活
Person的构造函数需要String作为第三个参数,但是您试图将int phone
传递给子类中的超级构造函数。 那是行不通的,因为它是错误的类型。
顺便说一句:您应该始终用字符串而不是整数来表示电话号码。
首先在所有类中将“电话号码”数据类型设置为整数。
主要功能将是:
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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.