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.

38 lines
962 B

/**
* 圆形类,继承自抽象图形类Shape
* 实现圆形的面积计算逻辑:面积 = π * 半径²
*/
public class Circle extends Shape {
// 圆形的半径
private double radius;
/**
* 构造方法:初始化圆形半径
* @param radius 半径(需大于0,此处做简单校验)
*/
public Circle(double radius) {
if (radius <= 0) {
throw new IllegalArgumentException("半径必须大于0!");
}
this.radius = radius;
}
/**
* 实现抽象方法:计算圆形面积
* @return 圆形的面积
*/
@Override
public double getArea() {
// Math.PI 是Java内置的π常量,精度更高
return Math.PI * radius * radius;
}
// Getter方法(可选,便于查看/修改半径)
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
}