定义一个接口AShape,用于图形计算,有计算周长方法getArea()和计算面积getArea()方法;编写实现接口类Rectangle,实现求矩形的周长和面积方法;在测试类Testmain中实现计算长为10,宽为8的的矩形周长和面积。
Shape类
public interface AShape {
double getP(int a,int b);//计算周长的方法
double getA(int a,int b);//计算面积的方法
}
Rectangle类
public class Rectangle implements AShape {
public double getP(int a, int b) {
return (a + b) * 2;
}
public double getA(int a, int b) {//计算面积的方法
return a * b;
}
}
Testmain类
public class Testmain {
public static void main(String[] args) {
Rectangle sc = new Rectangle();
double a = sc.getA(10,8);
double b = sc.getP(10,8);
System.out.println("矩形周长为:" + a);
System.out.println("矩形面积为:" + b);
}
}