设计一个 Shape 接口和它的两个实现类 Square 和 Circle,要求如下:
- Shape 接口中有一个抽象方法 area(),方法接收有一个 double 类型的参数,返回一个 double 类型的结果
- Square 和 Circle 中实现了 Shape 接口的 area()抽象方法,分别求正方形和圆形的面积并返回
- 在测试类中创建 Square 和 Circle 对象,计算边长为 2 的正方形面积和半径为 3 的圆形面积
interface Shape {
double area(double givenValue);
}
class Square implements Shape {
public double area(double sideLength) {
return sideLength * sideLength;
}
}
class Circle implements Shape {
public double area(double r) {
return Math.PI * r * r;
}
}
public class Test04 {
public static void main(String[] args) {
Shape square = new Square();
Shape circle = new Circle();
System.out.println(square.area(2));
System.out.println(circle.area(3));
}
}