码迷,mamicode.com
首页 > 编程语言 > 详细

p134 java

时间:2020-11-30 15:24:40      阅读:4      评论:0      收藏:0      [点我收藏+]

标签:static   class   circle   public   try   rgs   ext   string   double   


abstract class Geometry
{
public abstract double getArea();
}

class Pillar
{
Geometry bottom;
double height;
Pillar(Geometry bottom,double height)
{
this.bottom = bottom;
this.height=height;
}
public double getVolume()
{
if(bottom==null)
{
System.out.println("没有底,无法计算体积!");
return -1;
}
return bottom.getArea()*height; //求真正的体积;
}
}

class Circle extends Geometry
{
double r;
Circle(double rr)
{
this.r=rr;
}
public double getArea()
{
return 3.14*r*r;
}
}

class Rectangle extends Geometry
{
double a,b;
Rectangle(double aa,double bb)
{
this.a=aa;
this.b=bb;
}
public double getArea()
{
return a*b;
}

}

public class Appli {
public static void main(String []args)
{
Pillar pillar;
Geometry bottom=null;
pillar=new Pillar(bottom,100);
System.out.println("体积"+pillar.getVolume());
bottom=new Rectangle(12,22);
pillar=new Pillar(bottom,58);
System.out.println("体积"+pillar.getVolume());
bottom=new Circle(10);
pillar=new Pillar(bottom,58);
System.out.println("体积"+pillar.getVolume());
}

}

p134 java

标签:static   class   circle   public   try   rgs   ext   string   double   

原文地址:https://www.cnblogs.com/duanqibo/p/14032676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!