[英]Why I can pass null as argument to method without null being instance of accepted parameters?
[英]Why can't I pass an argument into a toString() method and why does it give a null?
在父類中,我有這個:
public String toString(String employeetype) {
return super.toString() + "\n" + employeetype + employeeno + " Hired on: "
+ DateFormat.getDateInstance().format(datehired) + " ";
}
在子類中,我有以下兩個:
public String toString() {
return super.toString("SalariedEmployee") + "[salary= " + this.pay() + "]";
}
public String toString() {
return super.toString("HourlyEmployee") + "[salary= " + this.pay() + "]";
}
當我給他們打電話時,它們會打印出來:
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 ]
那么,為什么說SalariedEmployeenull和HourlyEmployeeNull而不是SalariedEmployee和HourlyEmployeeN呢? 有沒有一種方法可以解決此問題,同時將代碼保持這種格式? 如果沒有,我為什么不能通過? 我想知道它是如何工作的。 謝謝你的幫助。
那么,為什么說SalariedEmployeenull和HourlyEmployeeNull而不是SalariedEmployee和HourlyEmployeeN呢?
因為employeeno
為null
。 為了使您的輸出更清晰,它與employeetype
之間應該有一個空格。
為什么employeeno
null? 在您給我們更多信息之前,我們不知道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.