給定如圖所示的Shape類、Circle類和Rectangle類的UML圖。其中Shape類是抽象類,它的getArea()是抽象方法。Circle和Rectangle是Shape的子類。
撰寫程式在com.demo包中定義上述類。在同一包中撰寫Test類,在main()方法中撰寫下面代碼:
double d = 0.0;
Shape[] shapes = new Shape[5];
for(int i=0; i< shapes.length;i++){
d = Math.random();
if(d>0.5){
shapes[i] = new Circle(1);
}else{
shapes[i] = new Rectangle(1,2);
}
}
double sumArea = sum(shapes);
System.out.printf("形狀的總面積=%.2f", sumArea);
}
這里:sum(Shpae[] shapes)方法是Test類的靜態方法,你需要定義該方法。
下面是程式的一次運行結果:
形狀的總面積=12.28
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/78922.html
標籤:Java SE
上一篇:mac中myeclipse2017ci配置tomcat9的問題
下一篇:出現這種錯誤是怎么一回事?
