3 changed files with 97 additions and 0 deletions
@ -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); |
|||
} |
|||
} |
|||
|
|||
@ -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 |
|||
Binary file not shown.
Loading…
Reference in new issue