commit
41aa9ebca3
8 changed files with 127 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,24 @@ |
|||||
|
一、实验目的 |
||||
|
1. 掌握抽象类的定义与使用 |
||||
|
2. 理解继承和多态的应用 |
||||
|
3. 学会统一处理不同图形对象 |
||||
|
|
||||
|
## 二、类图 |
||||
|
 |
||||
|
|
||||
|
三、AI使用记录 |
||||
|
代码结构设计 |
||||
|
|
||||
|
类图生成 |
||||
|
|
||||
|
报告撰写 |
||||
|
|
||||
|
四、组合 vs 继承 |
||||
|
继承:子类继承父类,代码复用简单,但耦合度高 |
||||
|
|
||||
|
组合:类包含其他类对象,灵活低耦合,但代码量多 |
||||
|
|
||||
|
本实验:图形是"is-a"关系,适合用继承 |
||||
|
|
||||
|
五、实验总结 |
||||
|
掌握了抽象类、继承和多态的应用,能够统一处理不同类型的图形对象。 |
||||
@ -0,0 +1,31 @@ |
|||||
|
@startuml |
||||
|
abstract class Shape { |
||||
|
{abstract} + getArea(): double |
||||
|
} |
||||
|
|
||||
|
class Circle { |
||||
|
- radius: double |
||||
|
+ getArea(): double |
||||
|
} |
||||
|
|
||||
|
class Rectangle { |
||||
|
- width: double |
||||
|
- height: double |
||||
|
+ getArea(): double |
||||
|
} |
||||
|
|
||||
|
class Triangle { |
||||
|
- base: double |
||||
|
- height: double |
||||
|
+ getArea(): double |
||||
|
} |
||||
|
|
||||
|
class ShapeUtil { |
||||
|
+ printArea(Shape): void |
||||
|
} |
||||
|
|
||||
|
Shape <|-- Circle |
||||
|
Shape <|-- Rectangle |
||||
|
Shape <|-- Triangle |
||||
|
ShapeUtil ..> Shape |
||||
|
@enduml |
||||
Loading…
Reference in new issue