爲什麼我運行此代碼時得到0而不是1500?Java - 爲什麼孩子不會繼承父母變量?
public class Department {
private double rate = 0.0;
public Department {
}
public getRate(){
return rate;
}
public setRate(){
rate = 1 + 2;
}
}
public class Employe extends Department {
private double salary = 0;
public Employe {
}
public calculateSalary(){
salary = getRate() * 500;
}
}
public static void main(String[] args) throws IOException {
Department department = New Department();
department.setRate();
Employe employe = new Employe();
System.out.println(employe.calculateSalary());
}
你確定代碼編譯完成嗎?它有很多的語法錯誤,比如你的構造函數定義中缺少括號,並且你沒有指定任何方法返回類型。 –