“Java作業 面相抽象原則 在一個列表中,放一個圓形對象,2個矩形對象,和3個三角形對象,并循環輸“長方形什么面相同

有一個寬為20米,高為16米的矩形閘門直立于水中,它的一邊與水面相齊,求閘門的一側所受的水壓力 。
重力加速度 g=10m/s^2
水密度=1000kg/m^3
高=16m
閘門的一側所受的平均水壓力
=(1/2) 水密度 * 重力加速度 * 高
=(1/2)*1000*10*16=80kPa
Java作業 面相抽象原則 在一個列表中,放一個圓形對象,2個矩形對象,和3個三角形對象,并循環輸
【“Java作業 面相抽象原則 在一個列表中,放一個圓形對象,2個矩形對象,和3個三角形對象,并循環輸“長方形什么面相同】import java.util.*;
public class RecTest {
public static void main(String[] args) {
ArrayList ag=new ArrayList<>();//集合列表!
ag.add(new Cir(5,"圓形1"));
ag.add(new Re(5,8,"矩形1"));
ag.add(new Re(4,10,"矩形2"));
ag.add(new Tr(3,12,"三角形1"));
ag.add(new Tr(12,5,"三角形2"));
ag.add(new Tr(11,5,"三角形3"));
for(ListIterator lt=ag.listIterator();lt.hasNext();) {
lt.next().area();
}
}
}
abstract class Geometry implements Comparable{
//默認公共屬性,寬,高!
protected double width,high;
protected String name;
Geometry(){}
Geometry(double width,double high,String name){
this.width=width;
this.high=high;
this.name=name;
}
protected void area() {
System.out.println(name "面積=" (width*high));
}
public int hashCode() {
return (int)(width);
}
public boolean equals(Object obj) {
if(!(obj instanceof Geometry))
return false;
Geometry o=(Geometry)obj;
return this.width==o.width;
}
public int compareTo(Geometry g) {
return Double.compare(this.width, g.width);
}
}//三角形!
class Tr extends Geometry{
Tr(double width,double high,String name){
super(width,high,name);
}
protected void area() {
System.out.println(name "面積=" (width*high/2));
}
}//矩形!
class Re extends Geometry{
Re(double width,double high,String name){
super(width,high,name);
}
}//圓!
class Cir extends Geometry{
Cir(double radius,String name){
super(radius,0,name);
}
public void area() {
System.out.println(name "面積=" (3.1415926*width*width));
}
}