实现Volume接口
设计圆形类Circle,给出计算周长和面积的方法。 声明圆柱体类Cylinder ,继承圆形类并实现Volume接口,计算体积。程序代码:
public class Circle { protected double radius; public Circle(double radius) { this.radius=radius; } public double perimeter() { return this.radius*2*3.14; } public double area() { return this.radius*this.radius*3.14; } public String toString() { return "一个圆,半径"+radius+"周长为"+perimeter()+"面积为"+area(); } public static void main(String args[]) { System.out.println(new Circle(12).toString()); } } public class Cylinder extends Circle implements Volume { protected double height; public Cylinder(double radius,double height) { super(radius); this.height=height; } public Cylinder() { this(0,0); } public double volume() { return super.area()*this.height; } public String toString() { return"体积为"+this.volume(); } public static void main(String args[]) { System.out.println(new Cylinder(12,12).toString()); } }
[ 本帖最后由 大剑 于 2011-12-12 23:56 编辑 ]