我想從Box中擴展一個Cube。我的程序首先有三個部分,我做一個矩形類然後我用它來擴展一個Box類然後我擴展一個Cube。我被困在Cube部分。這是我的指示說:我該怎麼辦「Box entends Cube」
評估說明 a。立方體是長度,寬度和高度均具有相同值的框。 b。您不必添加任何其他實例變量或方法,但您必須設置Cube的構造函數以確保長度,寬度和高度都具有相同的值。
矩形:
public class Rectangle
{
// instance variables
private int length;
private int width;
/**
* Constructor for objects of class rectangle
*/
public Rectangle(int l, int w)
{
// initialise instance variables
length = l;
width = w;
}
// return the height
public int getLength()
{
return length;
}
public int getWidth()
{
return width;
}
}
盒:
public class Box extends Rectangle
{
// instance variables
private int height;
/**
* Constructor for objects of class box
*/
public Box(int l, int w, int h)
{
// call superclass
super(l, w);
// initialise instance variables
height = h;
}
// return the height
public int getHeight()
{
return height;
}
}
和主要一個魔方:
class Cube extends Box{
// instance variables
private int height;
private int length;
private int width;
public Cube(int h,int w,int l){
super(h,w,l);
}
public double getheight(){
return height;
}
public double getlength() {
return length;
}
public double getwidth() {
return width;
}
}
我需要知道,如果我做了一個魔方正確與否。如果我沒有做對,請幫我修復它。
您複製了框中的所有內容。無論如何,什麼是立方體?它與盒子有什麼不同? – 2013-03-12 21:14:56
我將按照我的指示說的去編輯我的代碼。 – user2059140 2013-03-12 21:18:11
所以,指令說:「你不必添加任何額外的實例變量或方法。」但你做到了。爲什麼? – 2013-03-12 21:25:18