You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
1017 B
33 lines
1017 B
// 抽象类 Shape
|
|
abstract class Shape {
|
|
// 抽象方法,计算面积
|
|
public abstract double getArea();
|
|
}
|
|
|
|
// 工具类 ShapeUtil
|
|
class ShapeUtil {
|
|
// 打印图形的面积
|
|
public static void printArea(Shape shape) {
|
|
System.out.println("图形的面积为:" + shape.getArea());
|
|
}
|
|
}
|
|
|
|
// 主类 Main
|
|
public class ShapeCalculator {
|
|
public static void main(String[] args) {
|
|
// 创建不同的图形对象
|
|
Shape circle = new Circle(5.0); // 半径为5的圆
|
|
Shape rectangle = new Rectangle(4.0, 6.0); // 宽4、高6的矩形
|
|
Shape triangle = new Triangle(3.0, 8.0); // 底3、高8的三角形
|
|
|
|
// 使用ShapeUtil打印各图形的面积
|
|
System.out.println("圆的面积:");
|
|
ShapeUtil.printArea(circle);
|
|
|
|
System.out.println("矩形的面积:");
|
|
ShapeUtil.printArea(rectangle);
|
|
|
|
System.out.println("三角形的面积:");
|
|
ShapeUtil.printArea(triangle);
|
|
}
|
|
}
|