Java、调用方法和覆盖

Java, calling methods and override

我必须设计一个 class,它由 2 个子 class 组成。假设 A and B and B 也有 2 个 subs-classes。这部分真的不重要,因为我用不同的名字做了所有这些。

我正在努力尝试 运行 我的代码。我有代码,但出现 2 个错误。我不知道如何解决它。

错误如下:

aaa1.java:147: cannot find symbol
symbol : constructor Employee(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Employee
super(name, address, phone, email);
^
aaa1.java:179: cannot find symbol
symbol : constructor Employee(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Employee
super(name, address, phone, email);

我的代码:

import java.util.*;

public class aaa1 {
    public static void main(String[] args){
        Person p = new Person("J11h", "123 ABC St", "908-123-456", "asdf123@gmail.com");
        Student s = new Student("222se1h", "123 ABC St", "908-123-456", "asdf123@gmail.com", 1);
        Date date = new Date();
        Employee e = new Employee("wewesh", "123 ABC St", "908-123-456", "asdf123@gmail.com", "123", 2000.00, date);
        Faculty f = new Faculty("eewrr", "123 ABC St", "908-123-456", "asdf123@gmail.com", "1pm - 3pm", "Head of the department");
        Staff st = new Staff("rrereh", "123 ABC St", "908-123-456", "asdf123@gmail.com", "Assistant");
        System.out.println(p.toString());
        System.out.println(s.toString());
        System.out.println(e.toString());
        System.out.println(f.toString());
        System.out.println(st.toString());
    }
}

class Person {
    //A person has a name, address, phone number, and email address.
    String name;
    String address;
    String phone;
    String email;

    public Person(String name, String address, String phone, String email) {
        this.name = name;
        this.address = address;
        this.phone = phone;
        this.email = email;
    }

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getAddress(){
        return address;
    }

    public void setAddress(String address){
        this.address = address;
    }

    public String getPhone(){
        return phone;
    }

    public void setPhone(String phone){
        this.phone = phone;
    }

    public String getEmail(){
        return email;
    }

    public void setEmail(String email){
        this.email = email;
    }

    //Overriding
    public String toString(){
        return "The person's information is "+name+ ", " +address+ ", " +phone+ ", " +email;
    }
}

class Student extends Person{
    //A student has a class status
    //(freshman,sophomore, junior, or senior).
    //Define the status as a constant.
    final int freshman =1;
    final int sophomore =2;
    final int junior=3;
    final int senior=4;
    int status;

    public Student(String name, String address, String phone, String email, int status) {
        super(name, address, phone, email);
        this.status = status;
    }

    public int getStatus(){
        return status;
    }

    public void setStatus(int status){
        this.status = status;
    }

    //Overriding
    public String toString(){
        return "The student's information is: " +super.toString()+ "," + status;
    }
}

class Employee extends Person{
    //An employee has an office, salary, and date hired.
    String office;
    double salary;
    java.util.Date dateHired;

    public Employee(String name, String address, String phone, String email, String office, double salary, Date dateHired){
        super(name, address, phone, email);
        this.office = office;
        this.salary = salary;
        this.dateHired = dateHired;
    }

    public String getOffice(){
        return office;
    }

    public void setOffice(String office){
        this.office = office;
    }

    public double getSalary(){
        return salary;
    }

    public void setSalary(double salary){
        this.salary = salary;
    }

    public void setDateHired(Date dateHired){
        this.dateHired = dateHired;
    }

    //Overriding
    public String toString(){
        return "The Employee's information is: " +super.toString()+ ", " +office+ ", " +salary+ ", " +dateHired;
    }
}

class Faculty extends Employee{
    //A faculty member has office hours and a rank.
    String officeHours;
    String rank;

    public Faculty(String name, String address, String phone, String email, String officeHours, String rank){
        super(name, address, phone, email);
        this.officeHours = officeHours;
        this.rank = rank;
    }

    public String getOfficeHours(){
        return officeHours;
    }

    public void setOfficeHours(String officeHours){
        this.officeHours = officeHours;
    }

    public String getRank(){
        return rank;
    }

    public void setRank(String rank){
        this.rank = rank;
    }

    //Overriding
    public String toString(){
        return "The faculty's information is: " +super.toString()+ ", " + officeHours + ", " + rank;
    }
}

class Staff extends Employee{
    //A staff member has a title
    String title;

    public Staff(String name, String address, String phone, String email, String title){
        super(name, address, phone, email);
        this.title = title;
    }

    public String getTitle(){
        return title;
    }

    public void setTitle(String title){
        this.title = title;
    }

    //Overriding
    public String toString(){
        return "The staff's information is: " +super.toString()+ "," + title;
    }
}

你能帮帮我吗?

我相信,当你扩展它时,你还需要包括原始构造函数,所以你必须拥有与学生教职工和雇员中的一个人相同的构造函数(带有 @Override )

即使它只是调用 super();

所以这是一个构造函数(string,string,string,string); 员工(java.lang.String,java.lang.String,java.lang.String,java.lang.String)

Faculty - 第 147 行的 class - 扩展 Employee,因此您需要从 Employee:

调用超级构造函数
public Employee(String name, String address, String phone, String email, String office, double salary, Date dateHired){

即如果要实例化 Employee 的子 class,例如 Faculty.

,则需要传递所有这些参数

或者,您可以在 Employee 中声明另一个只接受这 4 个参数的构造函数。但是,您随后需要决定如何处理未传入的参数 - 您是简单地将它们设置为 null,还是将它们设置为安全的默认值?这取决于您的应用程序。

您的 Employee class 包含一个具有以下参数的构造函数:

public Employee(String name, String address, String phone, String email, String office, double salary, Date dateHired){

您的 FacultyStaff class 扩展 Employee,包含对 super:

的显式调用
super(name, address, phone, email);

super调用中的参数个数与Employeeclass构造函数中的参数个数不匹配;当它需要一个额外的 String、一个 double 和一个 java.util.Date.

时,您提供了四个字符串参数

想一想就会发现问题所在:

  • 教职员工的办公室、工资和受雇日期是什么时候?