多级继承默认构造函数

时间:2013-10-02 02:50:29

标签: java inheritance multi-level

父类是......

public class UMember {
    private String first;
    private String last;
    private String street;
    private String city;
    private String state;
    private String zipcode;

    public UMember() {
        this("na", "na", "na", "na", "na", "na");
    }

    public UMember(String first, String last, String street, 
            String city, String state, String zipcode){
        set(first, last, street, city, state, zipcode);
    }

    private void set(String first, String last, String street, 
            String city, String state, String zipcode){
        this.first = first;
        this.last = last;
        this.street = street;
        this.city = city;
        this.state = state;
        this.zipcode = zipcode;
    }

    public void setUMember(String first, String last, String street,
            String city, String state, String zipcode) {
       set(first, last, street, city, state, zipcode);
    }

UMember的父子类是......

public class Employee extends UMember {
    private int eid;
    private String doh;
    private String dept;
    private double salary;

    public Employee() {
        this(0, "na", "na", 0.0, "na", "na", "na", "na", "na", "na");
    }

    public Employee(int eid, String doh, String dept, double salary) {
        this(eid, doh, dept, salary, "na", "na", "na", "na", "na", "na");
    }



    public Employee(int eid, String doh, String dept, double salary, 
            String first, String last, String street, String city, 
            String state, String zipcode) {
        super(first, last, street, city, state, zipcode);
        set(eid, doh, dept, salary);
    }

    private void set(int eid, String doh, String dept, double salary) {
        this.setEid(eid);
        this.setDoh(doh);
        this.setDept(dept);
        this.setSalary(salary);
    }

    private void setEmployee(int eid, String doh, String dept, double salary, 
            String first, String last, String street, String city, 
            String state, String zipcode) {
        setUMember(first, last, street, city, state, zipcode);
        set(eid, doh, dept, salary);
    }

Employee的子类是......

public class Faculty extends Employee{
    private String rank;
    private int annualTeachingLoad;

    public Faculty() {
        this("na", 0, 0, "na", "na", 0.0, 
                "na", "na", "na", "na", "na", "na");
    }

    public Faculty(String rank, int annualTeachingLoad) {
        this(rank, annualTeachingLoad, 0, "na", "na", 0.0, 
                "na", "na", "na", "na", "na", "na");
    }

Faculty子类中的任何默认构造函数都不正确。我无法弄清楚为什么。我首先使用Faculty的默认值,然后是Employee,最后是UMember。任何关于为什么这两段代码都不正确的帮助将非常感激。

1 个答案:

答案 0 :(得分:2)

您正在使用this而不是super来调用构造函数。使用this调用构造函数只能调用在同一个类中声明的构造函数,而super用于调用超类中的构造函数。