【发布时间】: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