简体   繁体   中英

Why can't I pass an argument into a toString() method and why does it give a null?

In the parent class, I have this:

public String toString(String employeetype) {
    return super.toString() + "\n" + employeetype + employeeno + " Hired on: "
    + DateFormat.getDateInstance().format(datehired) + " ";
}

and in the child classes, I have these two:

public String toString() {
    return super.toString("SalariedEmployee") + "[salary= " + this.pay() + "]";
}
public String toString() {
    return super.toString("HourlyEmployee") + "[salary= " + this.pay() + "]";
}

When I call them, they print out:

Person [name=John Doe, ssno=012345678, gender=Male]
SalariedEmployeenull Hired on: Feb 25, 2014 [salary= $0.0 Per Week]
Person [name=John Doe, ssno=012345678, gender=Male]
HourlyEmployeenull Hired on: Feb 25, 2014 [salary= $0.0 Per Hour from ]

So why does say SalariedEmployeenull and HourlyEmployeeNull instead of SalariedEmployee and HourlyEmployee? Is there a way to fix this while keeping the code in this format? If not, why can't I pass as an argument? I'd like to know how it works. Thanks for your help.

So why does say SalariedEmployeenull and HourlyEmployeeNull instead of SalariedEmployee and HourlyEmployee?

Because employeeno is null . And to make your output clearer, there should be a space between it and employeetype .

Why is employeeno null? We can't know until you give us some more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM