从我的代码中,我希望我的卧室类能够继承超类 - 房间的长度和宽度。我已在填写评论内各行的错误。 我得到的错误,我找不到我的出路。请帮帮我?继承不起作用
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