Java,调用方法和覆盖

时间:2015-04-04 21:49:25

标签: java method-overriding

我必须设计一个由2个子类组成的类。让我们说A和B和B也有2个子类。这部分真的不重要,因为我用不同的名字做了所有这些。

我正在努力尝试运行我的代码。我有代码,但我得到2个错误。我无法弄清楚如何修复它。

错误如下:

  

aaa1.java:147:找不到符号
      symbol:构造函数Employee(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
      location:班级员工
                      超级(姓名,地址,电话,电子邮件);
                      ^
      aaa1.java:179:找不到符号
      symbol:构造函数Employee(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
      location:班级员工
                      超级(姓名,地址,电话,电子邮件);

我的代码:

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

你能帮帮我吗?

4 个答案:

答案 0 :(得分:2)

您的Employee类包含具有以下参数的构造函数:

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

您的FacultyStaff类扩展Employee,包含对super的显式调用:

super(name, address, phone, email);

super调用中的参数数量和Employee类构造函数中的参数数量不匹配;当您需要另外一个Stringdoublejava.util.Date时,您需要提供四个字符串参数。

答案 1 :(得分:1)

Faculty - 第147行的类 - 扩展Employee,因此您需要从Employee调用超级构造函数:

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

即。如果要实例化Employee的子类,例如Faculty,则需要传递所有这些参数。

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

答案 2 :(得分:0)

我相信,随着你扩展,你需要包括原始构造函数,所以你必须拥有与学生员工和员工(使用@Override)中的人相同的构造函数

即使只是调用super();

这是一个构造函数(字符串,字符串,字符串,字符串); 雇员(java.lang.String中,java.lang.String中,java.lang.String中,java.lang.String中)

答案 3 :(得分:0)

如果你想一会儿,你会发现问题:

  • 教职员工的职位,工资和雇用日期是什么?