diff --git a/w4/Circle.class b/w4/Circle.class new file mode 100644 index 0000000..d5e2493 Binary files /dev/null and b/w4/Circle.class differ diff --git a/w4/Circle.java b/w4/Circle.java new file mode 100644 index 0000000..b3a64bc --- /dev/null +++ b/w4/Circle.java @@ -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; + } +} diff --git a/w4/Rectangle.class b/w4/Rectangle.class new file mode 100644 index 0000000..4db70ea Binary files /dev/null and b/w4/Rectangle.class differ diff --git a/w4/Rectangle.java b/w4/Rectangle.java new file mode 100644 index 0000000..5674664 --- /dev/null +++ b/w4/Rectangle.java @@ -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; + } +} diff --git a/w4/Shape.class b/w4/Shape.class new file mode 100644 index 0000000..69b667a Binary files /dev/null and b/w4/Shape.class differ diff --git a/w4/Shape.java b/w4/Shape.java new file mode 100644 index 0000000..ac3b8fb --- /dev/null +++ b/w4/Shape.java @@ -0,0 +1,3 @@ +public abstract class Shape { + public abstract double getArea(); +} diff --git a/w4/ShapeUtil.class b/w4/ShapeUtil.class new file mode 100644 index 0000000..82575e4 Binary files /dev/null and b/w4/ShapeUtil.class differ diff --git a/w4/ShapeUtil.java b/w4/ShapeUtil.java new file mode 100644 index 0000000..51884ac --- /dev/null +++ b/w4/ShapeUtil.java @@ -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); + } +} diff --git a/w4/Triangle.class b/w4/Triangle.class new file mode 100644 index 0000000..f2bcdab Binary files /dev/null and b/w4/Triangle.class differ diff --git a/w4/Triangle.java b/w4/Triangle.java new file mode 100644 index 0000000..d81b1c5 --- /dev/null +++ b/w4/Triangle.java @@ -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; + } +} diff --git a/w4/实验报告.txt b/w4/实验报告.txt new file mode 100644 index 0000000..0a7a8bf --- /dev/null +++ b/w4/实验报告.txt @@ -0,0 +1,13 @@ +“组合 vs 继承”问题解析 + +维度 继承 (Inheritance) 组合 (Composition) +定义 子类“是一种”父类(is-a 关系),复用父类代码 类中包含其他类的对象(has-a 关系),复用对象功能 +耦合度 高,子类与父类强绑定,父类修改会影响所有子类 低,仅依赖对象的接口,修改不影响调用方 +灵活性 差,继承关系在编译期确定,无法动态修改 好,可在运行时替换组合的对象,适配不同场景 +适用场景 类之间有明确的层级关系,且父类接口稳定 仅需复用功能,无需共享接口,或需要动态切换实现 +本实验中的选择 使用继承,因为 Circle/Rectangle/Triangle 本质上“是一种” Shape,且需要统一的  getArea()  接口实现多态 不适合本场景,若用组合则无法实现统一的多态处理 + + +1. AI 使用情况:使用 AI 辅助梳理了抽象类与多态的实现逻辑,快速完成了基础代码框架和类图设计,并辅助理解了组合与继承的核心区别。 +2. 核心收获:通过重构,实现了图形的统一处理,新增图形时只需继承  Shape  类,无需修改  ShapeUtil  代码,符合开闭原则。 +3. 反思:继承适合有层级关系的场景,但过度继承会导致类爆炸;组合更灵活,但需要额外的接口抽象,实际开发中应优先考虑组合,必要时结合继承。 diff --git a/w4/类图.txt b/w4/类图.txt new file mode 100644 index 0000000..9986eb6 --- /dev/null +++ b/w4/类图.txt @@ -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  类实现统一处理。 \ No newline at end of file