You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1.9 KiB
1.9 KiB
图形面积计算器 - 类图
UML 类图(文本表示)
┌─────────────────────────┐
│ <<abstract>> │
│ Shape │
├─────────────────────────┤
├─────────────────────────┤
│ + getArea(): double │
└───────────┬─────────────┘
│
┌───────┼───────┐
│ │ │
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Circle │ │Rectangle│ │Triangle │
├─────────┤ ├─────────┤ ├─────────┤
│-radius │ │-width │ │-base │
│ │ │-height │ │-height │
├─────────┤ ├─────────┤ ├─────────┤
│+getArea()│ │+getArea()│ │+getArea()│
└─────────┘ └─────────┘ └─────────┘
类关系说明
Shape(抽象类)
- 方法:
getArea()- 抽象方法,返回图形面积
Circle(圆形)
- 属性:
radius- 半径 - 方法:
getArea()- 返回 π × r²
Rectangle(矩形)
- 属性:
width- 宽度,height- 高度 - 方法:
getArea()- 返回 宽 × 高
Triangle(三角形)
- 属性:
base- 底边,height- 高 - 方法:
getArea()- 返回 0.5 × 底 × 高
ShapeUtil(工具类)
- 方法:
printArea(Shape shape)- 统一打印图形面积
设计模式
使用了模板方法模式和多态:
- Shape 定义了计算面积的接口
- 各子类实现具体的面积计算逻辑
- ShapeUtil 通过多态统一处理所有图形