diff --git a/w5/ShapeUtil.java b/w5/ShapeUtil.java new file mode 100644 index 0000000..843e10f --- /dev/null +++ b/w5/ShapeUtil.java @@ -0,0 +1,51 @@ +abstract class Shape { + public abstract double getArea(); +} +class Circle extends Shape { + private double radius; + public Circle(double radius) { + this.radius = radius; + } + @Override + public double getArea() { + return Math.PI * radius * radius; + } +} +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; + } +} +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; + } +} +public class ShapeUtil { + public static void printArea(Shape shape) { + System.out.println("面积:" + shape.getArea()); + } + public static void main(String[] args) { + Shape circle = new Circle(6); + Shape rectangle = new Rectangle(9, 10); + Shape triangle = new Triangle(5, 10); + printArea(circle); + printArea(rectangle); + printArea(triangle); + } +} + \ No newline at end of file diff --git a/w5/shape_class_diagram.puml b/w5/shape_class_diagram.puml new file mode 100644 index 0000000..b579edf --- /dev/null +++ b/w5/shape_class_diagram.puml @@ -0,0 +1,46 @@ +@startuml +' 形状类图 + +' 抽象类 Shape +abstract class Shape { + + getArea(): double +} + +' Circle 类 +class Circle { + - radius: double + + Circle(radius: double) + + getArea(): double +} + +' Rectangle 类 +class Rectangle { + - width: double + - height: double + + Rectangle(width: double, height: double) + + getArea(): double +} + +' Triangle 类 +class Triangle { + - base: double + - height: double + + Triangle(base: double, height: double) + + getArea(): double +} + +' ShapeUtil 工具类 +class ShapeUtil { + + printArea(shape: Shape): void + + main(args: String[]): void +} + +' 继承关系 +Shape <|-- Circle +Shape <|-- Rectangle +Shape <|-- Triangle + +' 使用关系 +ShapeUtil --> Shape + +@enduml diff --git a/w5/实验报告.docx b/w5/实验报告.docx new file mode 100644 index 0000000..4d1a40f Binary files /dev/null and b/w5/实验报告.docx differ