底面接口形状的柱体

public class Fjava {

public static void main(String[] args)


{
Geometry a=new Circle(2);
Geometry b=new Rect(2,3);
Pillar v1=new Pillar(3,a);
Pillar v2=new Pillar(2,b);
v1.Volume();
v2.Volume();
}


}


public interface Geometry 
{
double getArea();
}


public class Pillar 
{
private int H;
private Geometry Dm;

public Pillar(int h,Geometry dm)
{
H=h;
Dm=dm;
}

public void Volume()
{
double v;
v=H*Dm.getArea();
System.out.println("柱体的体积为:"+v);
}

}


public class Rect implements Geometry
{
int Length;
int Wide;
Rect (int length,int wide)
{
Length=length;
Wide=wide;
}


public double getArea() 
{
return Length*Wide;
}
}


public class Circle implements Geometry
{
int Radius;

Circle(int r)
{
Radius=r;
}

public double getArea() 
{
return Math.PI*Radius*Radius;
}

}

你可能感兴趣的:(底面接口形状的柱体)