路过的大神帮帮忙
编写程序,定义一个接口Compute,声明计算周长calgirth()和面积calarea ()的方法,然后定义一个长方形类RRect和圆类Circle,两个类都实现接口Compute,最后编写测试类进行测试。
程序代码:
interface Compute{ double PI=3.141592654; double calGrith(); double calArea(); } class Rect implements Compute{ private double length; private double width; public Rect(double length,double width) { this.length=length; this.width=width; } public double calGrith() { return (length+width)*2; } public double calArea() { return length*width; } } class Circle implements Compute{ private double radius; public Circle(double radius) { this.radius=radius; } public double calGrith() { return 2*PI*radius; } public double calArea() { return PI*radius*radius; } } public class Test { public static void main(String[] args) { Rect rect=new Rect(2,4); System.out.println("rect.calGrith()="+rect.calGrith()); System.out.println("rect.calArea()="+rect.calArea()); System.out.println(); Circle circle=new Circle(1); System.out.println("circle.calGrith()="+circle.calGrith()); System.out.println("circle.calArea()="+circle.calArea()); } }