12 changed files with 93 additions and 0 deletions
Binary file not shown.
@ -0,0 +1,12 @@ |
|||||
|
public class Circle extends Shape { |
||||
|
private double radius; |
||||
|
|
||||
|
public Circle(double radius) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return Math.PI * radius * radius; |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,14 @@ |
|||||
|
public class Rectangle extends Shape { |
||||
|
private double width; |
||||
|
private double height; |
||||
|
|
||||
|
public Rectangle(double width, double height) { |
||||
|
this.width = width; |
||||
|
this.height = height; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return width * height; |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,3 @@ |
|||||
|
public abstract class Shape { |
||||
|
public abstract double getArea(); |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,15 @@ |
|||||
|
public class ShapeUtil { |
||||
|
public static void printArea(Shape shape) { |
||||
|
System.out.println("图形面积为:" + shape.getArea()); |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
Shape circle = new Circle(5); |
||||
|
Shape rectangle = new Rectangle(4, 6); |
||||
|
Shape triangle = new Triangle(3, 8); |
||||
|
|
||||
|
printArea(circle); |
||||
|
printArea(rectangle); |
||||
|
printArea(triangle); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,14 @@ |
|||||
|
public class Triangle extends Shape { |
||||
|
private double base; |
||||
|
private double height; |
||||
|
|
||||
|
public Triangle(double base, double height) { |
||||
|
this.base = base; |
||||
|
this.height = height; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return 0.5 * base * height; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
“组合 vs 继承”问题解析 |
||||
|
|
||||
|
维度 继承 (Inheritance) 组合 (Composition) |
||||
|
定义 子类“是一种”父类(is-a 关系),复用父类代码 类中包含其他类的对象(has-a 关系),复用对象功能 |
||||
|
耦合度 高,子类与父类强绑定,父类修改会影响所有子类 低,仅依赖对象的接口,修改不影响调用方 |
||||
|
灵活性 差,继承关系在编译期确定,无法动态修改 好,可在运行时替换组合的对象,适配不同场景 |
||||
|
适用场景 类之间有明确的层级关系,且父类接口稳定 仅需复用功能,无需共享接口,或需要动态切换实现 |
||||
|
本实验中的选择 使用继承,因为 Circle/Rectangle/Triangle 本质上“是一种” Shape,且需要统一的 getArea() 接口实现多态 不适合本场景,若用组合则无法实现统一的多态处理 |
||||
|
|
||||
|
|
||||
|
1. AI 使用情况:使用 AI 辅助梳理了抽象类与多态的实现逻辑,快速完成了基础代码框架和类图设计,并辅助理解了组合与继承的核心区别。 |
||||
|
2. 核心收获:通过重构,实现了图形的统一处理,新增图形时只需继承 Shape 类,无需修改 ShapeUtil 代码,符合开闭原则。 |
||||
|
3. 反思:继承适合有层级关系的场景,但过度继承会导致类爆炸;组合更灵活,但需要额外的接口抽象,实际开发中应优先考虑组合,必要时结合继承。 |
||||
@ -0,0 +1,22 @@ |
|||||
|
类图说明 |
||||
|
|
||||
|
plaintext |
||||
|
|
||||
|
┌─────────────┐ |
||||
|
│ Shape │ (抽象类) |
||||
|
├─────────────┤ |
||||
|
│+getArea():double│ |
||||
|
└──────┬──────┘ |
||||
|
│ |
||||
|
┌─────┴─────┬──────────┐ |
||||
|
│ │ │ |
||||
|
┌────────┐┌────────────┐┌────────────┐ |
||||
|
│ Circle ││ Rectangle ││ Triangle │ |
||||
|
├────────┤├────────────┤├────────────┤ |
||||
|
│-radius ││-width,-height││-base,-height│ |
||||
|
├────────┤├────────────┤├────────────┤ |
||||
|
│getArea()││ getArea() ││ getArea() │ |
||||
|
└────────┘└────────────┘└────────────┘ |
||||
|
|
||||
|
|
||||
|
- 核心关系: Shape 是抽象父类, Circle 、 Rectangle 、 Triangle 继承自 Shape ,实现 getArea() 方法; ShapeUtil 依赖 Shape 类实现统一处理。 |
||||
Loading…
Reference in new issue