Browse Source

上传文件至 'w5'

main
XuJiexian 3 weeks ago
parent
commit
8e873993b4
  1. 51
      w5/ShapeUtil.java
  2. 46
      w5/shape_class_diagram.puml
  3. BIN
      w5/实验报告.docx

51
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);
}
}

46
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

BIN
w5/实验报告.docx

Binary file not shown.
Loading…
Cancel
Save