如何將超類構造函數的某些參數繼承到子類構造函數? 例如,我只想將體重和身高繼承到子類,如何構造子類的約束函數?如何從超類繼承構造函數的部分到java中的子類
public abstract class People {
protected double weight;
protected int height;
protected String mood;
public People(double weight, int height, String mood) {
this.weight = weight;
this.mood = mood;
}
public class Health extends People {
private String bloodType;
public Health(double weight, int height, String bloodType){
super(weight,height); // this won't work
this.bloodType = bloodType;
}
'public Question' for a class called'People'?這甚至編譯? –
儘管沒有推薦,但您可以在「健康」類構造函數中爲此設置'super(weight,height,「」);',只是爲參數提供一個空的String值。否則更好的方法是在超/具體類中使用參數'(double weight,int height)'提供一個更多的構造函數 –