diff --git a/w5/Shape.java b/w5/Shape.java new file mode 100644 index 0000000..cebef67 --- /dev/null +++ b/w5/Shape.java @@ -0,0 +1,151 @@ +package com.example.shape; + +public abstract class Shape { + public abstract double getArea(); +} + +class Circle extends Shape { + private double radius; + private static final double PI = 3.1415926; + + public Circle(double radius) { + if (radius <= 0) { + throw new IllegalArgumentException("半径必须大于0"); + } + this.radius = radius; + } + + @Override + public double getArea() { + return PI * radius * radius; + } + + public double getRadius() { + return radius; + } + + public void setRadius(double radius) { + if (radius <= 0) { + throw new IllegalArgumentException("半径必须大于0"); + } + this.radius = radius; + } +} + +class Rectangle extends Shape { + private double length; + private double width; + + public Rectangle(double length, double width) { + if (length <= 0 || width <= 0) { + throw new IllegalArgumentException("长和宽必须大于0"); + } + this.length = length; + this.width = width; + } + + @Override + public double getArea() { + return length * width; + } + + public double getLength() { + return length; + } + + public void setLength(double length) { + if (length <= 0) { + throw new IllegalArgumentException("长必须大于0"); + } + this.length = length; + } + + public double getWidth() { + return width; + } + + public void setWidth(double width) { + if (width <= 0) { + throw new IllegalArgumentException("宽必须大于0"); + } + this.width = width; + } +} + +class Triangle extends Shape { + private double a; + private double b; + private double c; + + public Triangle(double a, double b, double c) { + // 校验三边>0,且满足三角形三边关系(任意两边之和>第三边) + if (a <= 0 || b <= 0 || c <= 0) { + throw new IllegalArgumentException("边长必须大于0"); + } + if (a + b <= c || a + c <= b || b + c <= a) { + throw new IllegalArgumentException("不满足三角形三边关系,无法构成三角形"); + } + this.a = a; + this.b = b; + this.c = c; + } + + @Override + public double getArea() { + double p = (a + b + c) / 2; + return Math.sqrt(p * (p - a) * (p - b) * (p - c)); + } + + public double getA() { + return a; + } + + public void setA(double a) { + if (a <= 0) { + throw new IllegalArgumentException("边长必须大于0"); + } + this.a = a; + if (a + b <= c || a + c <= b || b + c <= a) { + throw new IllegalArgumentException("不满足三角形三边关系,无法构成三角形"); + } + } + + public double getB() { + return b; + } + + public void setB(double b) { + if (b <= 0) { + throw new IllegalArgumentException("边长必须大于0"); + } + this.b = b; + if (a + b <= c || a + c <= b || b + c <= a) { + throw new IllegalArgumentException("不满足三角形三边关系,无法构成三角形"); + } + } + + public double getC() { + return c; + } + + public void setC(double c) { + if (c <= 0) { + throw new IllegalArgumentException("边长必须大于0"); + } + this.c = c; + if (a + b <= c || a + c <= b || b + c <= a) { + throw new IllegalArgumentException("不满足三角形三边关系,无法构成三角形"); + } + } +} + +class ShapeUtil { + public static void printArea(Shape shape) { + if (shape == null) { + System.out.println("图形对象不能为空!"); + return; + } + double area = shape.getArea(); + System.out.printf("该图形的面积为:%.2f%n", area); + } +} \ No newline at end of file diff --git a/w5/desktop.ini b/w5/desktop.ini new file mode 100644 index 0000000..71f71dd --- /dev/null +++ b/w5/desktop.ini @@ -0,0 +1,2 @@ +[LocalizedFileNames] +Shape.java=@Shape.java,0 diff --git a/w5/反思.txt b/w5/反思.txt new file mode 100644 index 0000000..91fc695 --- /dev/null +++ b/w5/反思.txt @@ -0,0 +1,3 @@ +1.本次重构通过抽象类+继承实现了代码的统一管理,解决了原独立类无法统一处理的问题,体现了面向对象多态的优势 +2.代码中增加了参数校验,避免了非法数据导致的计算错误,提升了健壮性 +3.工具类 ShapeUtil 通过多态实现了开闭原则:新增图形类(如正方形)无需修改工具类,只需新增 Shape 子类即可,符合面向对象设计原则。 \ No newline at end of file diff --git a/w5/实验报告.txt b/w5/实验报告.txt new file mode 100644 index 0000000..24146da --- /dev/null +++ b/w5/实验报告.txt @@ -0,0 +1,8 @@ +AI使用情况: +1.通过AI辅助更深刻地理解了抽象类,继承和多态的概念,以及Circle,Rectangle,Triangle三个子类如何继承并实现getArea方法 +2.用AI梳理了类的结构和UML类图的含义,知道我正确使用了@override方法 +3.利用AI检查程序漏洞,润色代码 + +组合VS继承问题: +继承是一种“is-a”的关系,具有高耦合度,编译期确定继承关系,灵活性较差,适用于类之间存在明确的父子关系。 +组合是一种“has-a”的关系,耦合度低,但运行期可动态替换组合对象,灵活性高,适用于从属关系 \ No newline at end of file diff --git a/w5/屏幕截图 2026-03-29 194820.png b/w5/屏幕截图 2026-03-29 194820.png new file mode 100644 index 0000000..ce33cf7 Binary files /dev/null and b/w5/屏幕截图 2026-03-29 194820.png differ diff --git a/w5/类图.jpg b/w5/类图.jpg new file mode 100644 index 0000000..91c05ff Binary files /dev/null and b/w5/类图.jpg differ