從我的代碼中,我希望我的臥室類能夠繼承超類 - 房間的長度和寬度。我已在填寫評論內各行的錯誤。 我得到的錯誤,我找不到我的出路。請幫幫我?繼承不起作用
package index;
public class Room {
int length;
int breadth;
Room(int x,int y)
{
length = x;
breadth = y;
}
int area(){
return (length*breadth);
}
}
class BedRoom extends Room{ //Implicit super constructor Room() is undefined for default constructor. Must define an explicit constructor
int height;
public void Bedroom(int x,int y,int z)
{
super(x,y); //Constructor call must be the first statement in a constructor
height=z;
}
int volume(){
return (length*breadth*height);
}
}
class InherTest
{
public static void main(String args[])
{
BedRoom room1 = new BedRoom(14,12,10); //The constructor BedRoom(int, int, int) is undefined
int area1 = room1.area();
int volume1 = room1.volume();
System.out.println("Area1 = "+area1);
System.out.println("Volume1 = "+volume1);
}
}
1+我錯過了大寫位,你抓到了。 – 2014-08-27 14:29:07
@HovercraftFullOfEels:和你一樣,爲了相反的原因:-) – 2014-08-27 14:29:38
將「public void Bedroom(int x,int y,int z)...」改爲「public BedRoom(int x,int y,int z )...「。 – Francesco 2014-08-27 14:34:24