我想知道如何在子類中使用超類構造函數,但需要在子類中實例化更少的屬性。下面是兩個班。我甚至不確定我目前是否在做正確的事情。在第二個類中,有一個錯誤提示「隱式超級構造函數PropertyDB()未定義,必須顯式調用另一個構造函數。」請注意,此代碼顯然不完整,並且有代碼被註釋掉。構造函數和繼承
public abstract class PropertyDB {
private int hectares;
private String crop;
private int lotWidth;
private int lotDepth;
private int buildingCoverage;
private int lakeFrontage;
private int numBedrooms;
private int listPrice;
//public abstract int getPricePerArea();
//public abstract int getPricePerBuildingArea();
public PropertyDB(int newHectares, String newCrop, int newLotWidth, int newLotDepth,
int newBuildingCoverage, int newLakeFrontage, int newNumBedrooms, int newListPrice){
hectares = newHectares;
crop = newCrop;
lotWidth = newLotWidth;
lotDepth = newLotDepth;
buildingCoverage = newBuildingCoverage;
lakeFrontage = newLakeFrontage;
numBedrooms = newNumBedrooms;
listPrice = newListPrice;
}
}
public class FarmedLand extends PropertyDB{
public FarmedLand(int newHectares, int newListPrice, String newCorn){
//super(270, 100, "corn");
hectares = newHectares;
listPrice = newListPrice;
corn = newCorn;
}
}
我仍然是初學者,但這似乎很有幫助。你能詳細解釋一下嗎? – 2012-03-09 03:30:02