我有兩個類如下。我想在單個構造函數TestEmployee()中訪問this()和super()構造函數。當前方法無法編譯。在Java中有沒有其他的方法,所以我可以在同一個構造函數體中調用this()和super()。在Java中,有沒有什麼方法可以在同一個構造函數中調用this()和super()?
class Employee{
double salary;
Employee(double salary){
this.salary = salary;
}
}
class TestEmployee extends Employee{
TestEmployee(){
super(1000000);
this(10000);
}
double bonus;
TestEmployee(double bonus){
this.bonus = bonus;
}
}
任何其他方法叫在同一個構造函數中調用'super()'和'this()'沒有任何意義......最好有一個構造函數接受'salary'和'bonus'兩個參數並調用'this(10000,10000) 。 –
請注意,你的'TestEmployee(double bonus)'實際上是這樣的:'{super(); this.bonus =獎金; }這意味着你會調用兩個超類的構造函數。這就是爲什麼這種功能沒有意義。 –