(Java大学实用教程)第五章例题5例子10

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

abstract class Geometry
{
public abstract double ComputerArea();
}
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 ComputerArea()
{
return((1/2.0)*(a+b)*h);
}
}
class Circle extends Geometry
{
double r;
Circle(double r)
{
this.r=r;
}
public double ComputerArea()
{
return(3.14*r*r);
}
}
class Cone
{
Geometry bottom;
double height;
Cone(Geometry bottom,double height)
{
this.bottom=bottom;
this.height=height;
}
void changBottom(Geometry bottom)
{
this.bottom=bottom;
}
public double ComputerVolume()
{
return (puterArea()*height)/3.0;
}
}
public class Example
{
public static void main(String args[])
{
Cone cone;
Geometry geometry;
geometry=new Lader(2.0,7.0,10.7);
System.out.println("梯形的面积"+puterArea());
cone=new Cone(geometry,30);
System.out.println("梯形底的锥的体积"+puterVolume());
geometry=new Circle(10);
System.out.println("半径是10的圆的面积"+puterArea());
cone.changBottom(geometry);
System.out.println("圆形底的锥的体积"+puterVolume());
}
}

相关文档
最新文档