java(面向抽象)

abstract class Geometry{
 public abstract double getArea();
}
class Pillar{
 Geometry bottom;
 double height;
 Pillar(Geometry bottom,double height){
  this.bottom=bottom;
  this.height=height;
 }
 void changeBottom(Geometry bottom){
  this.bottom=bottom;
 }
 public double getVolume(){
  return bottom.getArea()*height;
 }
}
class Lader extends Geometry{
 double a,b,h;
 Lader(double a,double b,double h){
  this.a=a;
  this.b=b;
  this.h=h;
 }
 public double getArea(){
  return((1/2.0)*(a+b)*h);
 }
}
class Circle extends Geometry{
 double r;
 Circle(double r){
  this.r=r;
 }
 public double getArea(){
  return(3.14*r*r);
 }
}
public class Example5_10{
 public static void main(String args[]){
  Pillar pillar;
  Geometry tuxing;
  tuxing=new Lader(12,22,100);
  System.out.println("梯形的面积是"+tuxing.getArea());
  pillar=new Pillar(tuxing,58);
  System.out.println("梯形底的柱体的面积"+pillar.getVolume());
  tuxing=new Circle(10);
  System.out.println("半径是10的圆形的面积是"+tuxing.getArea());
  pillar.changeBottom(tuxing);
  System.out.println("圆形底的柱体的面积"+pillar.getVolume());
 }
}
  

 

posted @ 2013-04-14 21:22  徐慧同学  阅读(143)  评论(0编辑  收藏  举报