在子类中使用父类构造函数的困难

时间:2016-12-13 01:51:31

标签: java oop inheritance extends

我有一个Contractor子类,扩展了它的父类Staff

    Test Class

    Date macHireDate = new Date(2016, 1, 5);
    Staff mac = new Staff("Mac", 66222222, macHireDate);
    System.out.println(mac);

    Staff bob = new Contractor("Bob", new Date(2012,10,10), new Date(2013,4,11))
    System.out.println(bob);

我希望输出为Bob (ID: None) ... 但我不确定如何将ID作为字符串。我不想改变我的测试类中的任何内容

public class Contractor extends Staff {
private Date contractEnds;

这是我的Contractor类构造函数

public Contractor(String name, Date hireDate, long employementID, Date contractEnds) {
    super(name, employementID, hireDate);
    this.contractEnds = contractEnds;
}

并且它延伸了

public Staff(String name, long employementID, Date hireDate) {
super();
this.name = name;
this.hireDate = hireDate;
this.employementID = employementID;
}

Bob只有String,Date,Date。所以我在如何制作bobs ID方面遇到了困难。没有"不改变测试类中的任何内容。

4 个答案:

答案 0 :(得分:1)

如果您将Contractor的构造函数更改为

public Contractor(String name, Date hireDate, Date contractEnds) {
    super(name, 0, hireDate);
    this.contractEnds = contractEnds;
}

然后还更改toString Staff employementID方法,这样如果None为0则会打印{{1}}

答案 1 :(得分:1)

使用super()构造函数和需要初始化的任何其他附加参数的组合来实例化对象。

public Contractor(String name, Date hireDate, Date contractEnds, int employmentID ) {
    super(name, employmentID, hireDate);
    this.contractEnds = contractEnds;
}

super()构造函数继承了所有超类的构造函数代码,并初始化了子类中的所有相应变量

您还可以在方法中添加@Override注释来编写自定义代码 例如:

@Override
public String toString(){...}

答案 2 :(得分:0)

将构造函数添加到不需要ID的Contractor中。使用"这个"使用默认ID调用主构造函数:

[警告 - 未经测试的代码]

public Contractor(String name, Date hireDate, Date contractEnds) {
    this(name, hireDate, 0, contractEnds);
}

您需要选择long类型的某些值来表示不存在的ID。我使用了0

答案 3 :(得分:0)

以下代码:

public class JavaApplication23
{
    public static void main(String[] args)
    {
        Date macHireDate = new Date(2016, 1, 5);
        Staff mac = new Staff("Mac", 66222222, macHireDate);
        System.out.println(mac);

        Staff bob = new Contractor("Bob", new Date(2012,10,10), 1234L, new Date(2013,4,11));
        System.out.println(bob);

        Staff bob2 = new Contractor("Bob2", new Date(2012,10,10), new Date(2013,4,11));
        System.out.println(bob2);
    }

}

class Contractor extends Staff
{
    private Date contractEnds;

    public Contractor(String name, Date hireDate, long employementID, Date contractEnds)
    {
        super(name, employementID, hireDate);
        this.contractEnds = contractEnds;
    }

    public Contractor(String name, Date hireDate, Date contractEnds)
    {
        super(name, hireDate);
        this.contractEnds = contractEnds;
    }

    @Override
    public String toString()
    {
        String out = super.toString();
        return out + "ContractEnds: " + contractEnds + "\n";
    }
}

class Staff
{
    private String name;
    private long employementID;
    private boolean idIsOK;
    private Date hireDate;

    Staff(String name, long employementID, Date hireDate)
    {
        //super();
        this.name = name;
        this.hireDate = hireDate;
        this.employementID = employementID;
        idIsOK = true;
    }

    Staff(String name, Date hireDate)
    {
        //super();
        this.name = name;
        this.hireDate = hireDate;
        this.employementID = -1;
        idIsOK = false;
    }

    @Override
    public String toString()
    {
        String out = "Name: " + name + "\n";
        out += "HireDate: " + hireDate + "\n";
        out += "EmployementID: " + ((idIsOK) ? employementID : "None") + "\n";
        return out;
    }
}

返回:

Name: Mac
HireDate: Sat Feb 05 00:00:00 CET 3916
EmployementID: 66222222

Name: Bob
HireDate: Sun Nov 10 00:00:00 CET 3912
EmployementID: 1234
ContractEnds: Sun May 11 00:00:00 CEST 3913

Name: Bob2
HireDate: Sun Nov 10 00:00:00 CET 3912
EmployementID: None
ContractEnds: Sun May 11 00:00:00 CEST 3913