本文详解如何为 Circle 类设计 add(Circle other) 方法,使其将当前圆与传入圆的面积之和,映射为一个等效的新圆,并原地更新当前圆的半径,支持链式调用。
本文详解如何为 circle 类设计 `add(circle other)` 方法,使其将当前圆与传入圆的面积之和,映射为一个等效的新圆,并原地更新当前圆的半径,支持链式调用。
在面向对象建模中,add 方法不应简单执行数值相加,而应体现领域语义——本题明确要求:“两圆相加”定义为生成一个新圆,其面积等于原两圆面积之和。由于圆面积公式为 $A = \pi r^2$,因此若当前圆半径为 $r_1$,另一圆为 $r_2$,则合并后等效半径 $r_3$ 需满足:
$$ \pi r_3^2 = \pi r_1^2 + \pi r_2^2 \quad \Rightarrow \quad r_3 = \sqrt{r_1^2 + r_2^2} $$
注意:$\pi$ 在推导中被约去,实际计算无需显式参与,既提升精度又避免浮点冗余。
以下是符合题目要求(支持 c1.add(c2).add(c3) 链式调用、原地修改、返回 this)的完整实现:
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
/**
* 将当前圆与参数圆的面积相加,并更新当前圆半径为等效半径。
* 支持链式调用,返回 this。
*
* @param other 待合并的 Circle 对象(不可为 null)
* @return 当前 Circle 实例(便于链式操作)
*/
public Circle add(Circle other) {
if (other == null) {
throw new IllegalArgumentException("Parameter 'other' cannot be null");
}
// 核心计算:r_new = √(r₁² + r₂²)
double newRadius = Math.sqrt(this.radius * this.radius + other.radius * other.radius);
this.radius = newRadius;
return this; // 关键:返回 this 以支持链式调用(如 c1.add(c2).add(c3))
}
}✅ 关键设计说明:
- 返回类型为 Circle 而非 void 或 double:这是实现链式调用(fluent interface)的前提。c1.add(c2).add(c3) 能成立,正是因为每次 add() 都返回 this。
- 原地修改(mutating)而非创建新对象:题目示例中 c1.add(c2).add(c3) 后仅 c1 的半径变化,c2 和 c3 保持不变,证实该方法应修改调用者自身状态。
- 无需 PI 参与计算:如数学推导所示,$\pi$ 在等式两边自然消去。引入 Math.PI 不但多余,还可能因浮点误差引入微小偏差(尽管影响极小,但违背简洁性与精确性原则)。
- 空值防护:添加 null 检查,增强鲁棒性,符合生产级代码规范。
⚠️ 常见误区纠正:
- ❌ 错误理解为“重载 + 运算符”:Java 不支持运算符重载,add 是普通方法名,语义由开发者定义。
- ❌ 返回 double 或 void:将导致 c1.add(c2).add(c3) 编译失败(前者无 .add() 方法,后者无法连续调用)。
- ❌ 修改传入对象(如 other.radius = ...):违反封装原则,且与预期输出(仅 c1 变化)矛盾。
最后,配合给出的 TestCircle 主类运行,输出完全匹配预期:
Before adding, Radius of the first circle is 3.5 Radius of the second circle is 6.8 Radius of the third circle is 12.9 After adding, Radius of the first circle is 14.996666296213968 Radius of the second circle is 6.8 Radius of the third circle is 12.9
该实现兼顾数学正确性、API 可用性与代码健壮性,是面向对象设计中“行为即语义”的典型实践。










