

問題如上,小白求一個好的方法來實作。不太懂java相關語法。
public class Rectangle
{
private int length;
private int width;
public Rectangle(int length, int width)
{
this.length = length;
this.width = width;
}
public int calArea()
{
return length * width;
}
}
public class Cube extends Rectangle
{
private int height;
public Cube(int length, int width, int height)
{
//在此處添加代碼為類的所有屬性賦值;在提交函式實作代碼時,請洗掉此注釋
}
public int calArea()
{
//在此處添加代碼,回傳Cube的體積;在提交函式實作代碼時,請洗掉此注釋
}
}
uj5u.com熱心網友回復:
這不是 java 最基礎的知識嗎?public class Rectangle
{
private int length;
private int width;
public Rectangle(int length, int width)
{
this.length = length;
this.width = width;
}
public int calArea()
{
return length * width;
}
}
public class Cube extends Rectangle
{
private int height;
public Cube(int length, int width, int height)
{
super(length, width); //呼叫父類構造方法
this.height = height;
}
public int calArea()
{
return super.calArea()*height; //呼叫父類算面積的方法
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/188022.html
標籤:Java相關
