commit
5e9fa21b4d
7 changed files with 119 additions and 0 deletions
@ -0,0 +1,15 @@ |
|||||
|
package ShapeAreaCalculator; |
||||
|
|
||||
|
public class Circle extends Shape { |
||||
|
private double radius; |
||||
|
|
||||
|
public Circle(double radius) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return Math.PI * radius * radius; |
||||
|
} |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,13 @@ |
|||||
|
package ShapeAreaCalculator; |
||||
|
|
||||
|
public class Main { |
||||
|
public static void main(String[] args) { |
||||
|
Shape circle = new Circle(5); |
||||
|
Shape rectangle = new Rectangle(4, 6); |
||||
|
Shape triangle = new Triangle(3, 4); |
||||
|
|
||||
|
ShapeUtil.printArea(circle); |
||||
|
ShapeUtil.printArea(rectangle); |
||||
|
ShapeUtil.printArea(triangle); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
package ShapeAreaCalculator; |
||||
|
|
||||
|
public 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; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,5 @@ |
|||||
|
package ShapeAreaCalculator; |
||||
|
|
||||
|
public abstract class Shape { |
||||
|
public abstract double getArea(); |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
package ShapeAreaCalculator; |
||||
|
|
||||
|
public class ShapeUtil { |
||||
|
public static void printArea(Shape shape) { |
||||
|
System.out.println("图形面积: " + shape.getArea()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
package ShapeAreaCalculator; |
||||
|
|
||||
|
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; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,47 @@ |
|||||
|
@startuml |
||||
|
' 设置样式 |
||||
|
skinparam classAttributeIconSize 0 |
||||
|
skinparam backgroundColor #FEFEFE |
||||
|
skinparam class { |
||||
|
BackgroundColor #E1F5FE |
||||
|
BorderColor #0288D1 |
||||
|
ArrowColor #0288D1 |
||||
|
} |
||||
|
|
||||
|
abstract class Shape { |
||||
|
{abstract} + getArea(): double |
||||
|
} |
||||
|
|
||||
|
class Circle { |
||||
|
- radius: double |
||||
|
+ Circle(radius: double) |
||||
|
+ getArea(): double |
||||
|
} |
||||
|
|
||||
|
class Rectangle { |
||||
|
- width: double |
||||
|
- height: double |
||||
|
+ Rectangle(width: double, height: double) |
||||
|
+ getArea(): double |
||||
|
} |
||||
|
|
||||
|
class Triangle { |
||||
|
- base: double |
||||
|
- height: double |
||||
|
+ Triangle(base: double, height: double) |
||||
|
+ getArea(): double |
||||
|
} |
||||
|
|
||||
|
class ShapeUtil { |
||||
|
+ {static} printArea(shape: Shape): void |
||||
|
} |
||||
|
|
||||
|
' 继承关系 |
||||
|
Shape <|-- Circle |
||||
|
Shape <|-- Rectangle |
||||
|
Shape <|-- Triangle |
||||
|
|
||||
|
' 依赖关系 |
||||
|
ShapeUtil ..> Shape : uses |
||||
|
|
||||
|
@enduml |
||||
Loading…
Reference in new issue