Browse Source

feat:W3

main
Wengxiyi 3 weeks ago
parent
commit
62b096b97f
  1. BIN
      W3/Circle.class
  2. 12
      W3/Circle.java
  3. BIN
      W3/Rectangle.class
  4. 14
      W3/Rectangle.java
  5. BIN
      W3/Shape.class
  6. 3
      W3/Shape.java
  7. 41
      W3/ShapeClassDiagram.md
  8. BIN
      W3/ShapeUtil.class
  9. 13
      W3/ShapeUtil.java
  10. BIN
      W3/Triangle.class
  11. 14
      W3/Triangle.java
  12. 3
      W3/实验报告.txt

BIN
W3/Circle.class

Binary file not shown.

12
W3/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;
}
}

BIN
W3/Rectangle.class

Binary file not shown.

14
W3/Rectangle.java

@ -0,0 +1,14 @@
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double getArea() {
return length * width;
}
}

BIN
W3/Shape.class

Binary file not shown.

3
W3/Shape.java

@ -0,0 +1,3 @@
public abstract class Shape {
public abstract double getArea();
}

41
W3/ShapeClassDiagram.md

@ -0,0 +1,41 @@
# 形状类图
```
+-------------------+
| Shape |
+-------------------+
| +getArea():double |
+-------------------+
^
| 继承
+---------+----------+
| |
v v
+----------------+ +-------------------+ +----------------+
| Circle | | Rectangle | | Triangle |
+----------------+ +-------------------+ +----------------+
| -radius:double | | -length:double | | -base:double |
| | | -width:double | | -height:double |
+----------------+ +-------------------+ +----------------+
| +getArea():double | | +getArea():double | | +getArea():double |
+----------------+ +-------------------+ +----------------+
+-------------------+
| ShapeUtil |
+-------------------+
| +printArea(Shape):void |
| +main(String[]):void |
+-------------------+
```
## 类说明
1. **Shape** - 抽象类,定义了计算面积的抽象方法
2. **Circle** - 圆形,继承自 Shape,使用 π×r² 计算面积
3. **Rectangle** - 矩形,继承自 Shape,使用长×宽计算面积
4. **Triangle** - 三角形,继承自 Shape,使用 0.5×底×高计算面积
5. **ShapeUtil** - 工具类,用于测试不同形状的面积计算
## 多态特性
ShapeUtil.printArea() 方法接收 Shape 类型参数,但运行时会根据实际传入的子类对象调用对应的 getArea() 方法,体现了 Java 的多态特性。

BIN
W3/ShapeUtil.class

Binary file not shown.

13
W3/ShapeUtil.java

@ -0,0 +1,13 @@
public class ShapeUtil {
public void printArea(Shape shape) {
System.out.println("图形的面积为:" + shape.getArea());
}
public static void main(String[] args) {
ShapeUtil util = new ShapeUtil();
util.printArea(new Circle(5));
util.printArea(new Rectangle(4, 3));
util.printArea(new Triangle(3, 4));
}
}

BIN
W3/Triangle.class

Binary file not shown.

14
W3/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;
}
}

3
W3/实验报告.txt

@ -0,0 +1,3 @@
通过AI先编写了完整程序,并要求AI一步步讲解代码。随后自行操作,通过AI查找错误点,并运用AI绘制类图并讲解意义。
继承:子类和父辈强绑定
组合:类之间依赖松散
Loading…
Cancel
Save