4 changed files with 102 additions and 0 deletions
@ -0,0 +1,9 @@ |
|||||
|
图形面积计算器重构实验反思 |
||||
|
|
||||
|
本次实验通过抽象类Shape与继承重构图形面积计算器,加深了对面向对象思想的理解。 |
||||
|
|
||||
|
通过定义抽象方法getArea(),让Circle、Rectangle、riangle子类实现各自面积计算,再借助ShapeUtil工具类统一处理,既实现了代码复用,也体现了多态的优势。 |
||||
|
|
||||
|
实验中曾因类名与文件名不一致导致编译报错,也发现了类职责划分不清、缺少参数校验等问题。后续将严格遵守Java语法规范,优化类设计,补充异常处理,进一步提升代码健壮性与可维护性。 |
||||
|
|
||||
|
|
||||
@ -0,0 +1,55 @@ |
|||||
|
abstract class Shape { |
||||
|
public abstract double getArea(); |
||||
|
} |
||||
|
|
||||
|
class Circle extends Shape { |
||||
|
private double radius; |
||||
|
public Circle(double radius) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
public double getArea() { |
||||
|
return Math.PI * radius * radius; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class Rectangle extends Shape { |
||||
|
private double length; |
||||
|
private double width; |
||||
|
public Rectangle(double length, double width) { |
||||
|
this.length = length; |
||||
|
this.width = width; |
||||
|
} |
||||
|
public double getArea() { |
||||
|
return length * width; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class Triangle extends Shape { |
||||
|
private double base; |
||||
|
private double height; |
||||
|
public Triangle(double base, double height) { |
||||
|
this.base = base; |
||||
|
this.height = height; |
||||
|
} |
||||
|
public double getArea() { |
||||
|
return 0.5 * base * height; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
class ShapeUtil { |
||||
|
public static void printArea(Shape shape) { |
||||
|
System.out.println(shape.getArea()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 关键修改:类名与文件名完全一致
|
||||
|
public class 图形面积计算器重构 { |
||||
|
public static void main(String[] args) { |
||||
|
Shape circle = new Circle(2); |
||||
|
Shape rectangle = new Rectangle(3, 4); |
||||
|
Shape triangle = new Triangle(3, 4); |
||||
|
ShapeUtil.printArea(circle); |
||||
|
ShapeUtil.printArea(rectangle); |
||||
|
ShapeUtil.printArea(triangle); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
Al使用情况 |
||||
|
•借助AI完成抽象类设计、子类继承实现、 |
||||
|
工具类封装的代码框架 |
||||
|
·排查并解决了「类名与文件名不一致」「构 |
||||
|
造方法名错误」等编译问题 |
||||
|
|
||||
|
维度 继承(Inheritance) 组合(Composition) |
||||
|
关系本质 关系(子类是父类的一种) 关系(整体包含部分) |
||||
|
耦合度 高,子类依赖父类实现,修改父类易影响子类 低,通过接口交互,修改内部实现不影响外部 |
||||
|
复用性 代码复用,但受限于单继承 更高,可灵活组合多个对象 |
||||
|
适用场景 类间存在明确的「种类」关系(如 Circle is a Shape) 类间存在「包含」关系(如 Car has a Engine) |
||||
@ -0,0 +1,27 @@ |
|||||
|
+----------------+ |
||||
|
| Shape | |
||||
|
+----------------+ |
||||
|
| + getArea(): double | |
||||
|
+----------------+ |
||||
|
^ |
||||
|
| |
||||
|
+------+------+------+ |
||||
|
| | | |
||||
|
+--------+ +----------+ +----------+ |
||||
|
| Circle | | Rectangle| | Triangle | |
||||
|
+--------+ +----------+ +----------+ |
||||
|
| - radius: double | | - length: double | | - base: double | |
||||
|
| + Circle(radius: double) | | + Rectangle(length: double, width: double) | | + Triangle(base: double, height: double) | |
||||
|
| + getArea(): double | | + getArea(): double | | + getArea(): double | |
||||
|
+--------+ +----------+ +----------+ |
||||
|
|
||||
|
+----------------+ |
||||
|
| ShapeUtil | |
||||
|
+----------------+ |
||||
|
| + printArea(shape: Shape): void | |
||||
|
+----------------+ |
||||
|
| |
||||
|
v |
||||
|
+----------------+ |
||||
|
| Shape | |
||||
|
+----------------+ |
||||
Loading…
Reference in new issue