java继承抽象类abstract关键字 图形实例

测试类
class AbstractDemo{
public static void main(String[] args) {
Circle cc=new Circle(5.6);
cc.getArea();
Rectangle rt=new Rectangle(8,6);
rt.getArea();
Trapezoid tz=new Trapezoid(5,10,4);
tz.getArea();
}
}

父类抽象类
abstract class Shape{
public abstract void getArea();

}

子类 圆

class Circle extends Shape{
final double PI=3.1415926;
double dRadius;
public void getArea(){
System.out.println(“圆的面积为:”+PIdRadiusdRadius);
}
public Circle(double r) {
this.dRadius=r;
}
}

子类 矩形

class Rectangle extends Shape{
double dLength;
double dWidth;
public void getArea(){
System.out.println(“矩形的面积为:”+dLength*dWidth);
}
public Rectangle(double l,double w){
this.dLength=l;
this.dWidth=w;
}
}

子类 梯形

class Trapezoid extends Shape{
double upBase;
double downBase;
double height;
public void getArea() {
System.out.println(“梯形的面积为:”+((upBase+downBase)*height)/2);}
public Trapezoid(double u,double d,double h) {
this.upBase=u;
this.downBase=d;
this.height=h;
}
}

结果

java继承抽象类abstract关键字 图形实例