12 changed files with 100 additions and 0 deletions
Binary file not shown.
@ -0,0 +1,12 @@ |
|||||
|
public class Circle extends Shape { |
||||
|
private double radius; |
||||
|
|
||||
|
public Circle(double radius) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return Math.PI * radius * radius; |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,14 @@ |
|||||
|
public class Rectangle extends Shape { |
||||
|
private double length; |
||||
|
private double width; |
||||
|
|
||||
|
public Rectangle(double length, double width) { |
||||
|
this.length = length; |
||||
|
this.width = width; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return length * width; |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,3 @@ |
|||||
|
public abstract class Shape { |
||||
|
public abstract double getArea(); |
||||
|
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
# 形状类图 |
||||
|
|
||||
|
``` |
||||
|
+-------------------+ |
||||
|
| Shape | |
||||
|
+-------------------+ |
||||
|
| +getArea():double | |
||||
|
+-------------------+ |
||||
|
^ |
||||
|
| 继承 |
||||
|
+---------+----------+ |
||||
|
| | |
||||
|
v v |
||||
|
+----------------+ +-------------------+ +----------------+ |
||||
|
| Circle | | Rectangle | | Triangle | |
||||
|
+----------------+ +-------------------+ +----------------+ |
||||
|
| -radius:double | | -length:double | | -base:double | |
||||
|
| | | -width:double | | -height:double | |
||||
|
+----------------+ +-------------------+ +----------------+ |
||||
|
| +getArea():double | | +getArea():double | | +getArea():double | |
||||
|
+----------------+ +-------------------+ +----------------+ |
||||
|
|
||||
|
+-------------------+ |
||||
|
| ShapeUtil | |
||||
|
+-------------------+ |
||||
|
| +printArea(Shape):void | |
||||
|
| +main(String[]):void | |
||||
|
+-------------------+ |
||||
|
``` |
||||
|
|
||||
|
## 类说明 |
||||
|
|
||||
|
1. **Shape** - 抽象类,定义了计算面积的抽象方法 |
||||
|
2. **Circle** - 圆形,继承自 Shape,使用 π×r² 计算面积 |
||||
|
3. **Rectangle** - 矩形,继承自 Shape,使用长×宽计算面积 |
||||
|
4. **Triangle** - 三角形,继承自 Shape,使用 0.5×底×高计算面积 |
||||
|
5. **ShapeUtil** - 工具类,用于测试不同形状的面积计算 |
||||
|
|
||||
|
## 多态特性 |
||||
|
|
||||
|
ShapeUtil.printArea() 方法接收 Shape 类型参数,但运行时会根据实际传入的子类对象调用对应的 getArea() 方法,体现了 Java 的多态特性。 |
||||
Binary file not shown.
@ -0,0 +1,13 @@ |
|||||
|
public class ShapeUtil { |
||||
|
|
||||
|
public void printArea(Shape shape) { |
||||
|
System.out.println("图形的面积为:" + shape.getArea()); |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
ShapeUtil util = new ShapeUtil(); |
||||
|
util.printArea(new Circle(5)); |
||||
|
util.printArea(new Rectangle(4, 3)); |
||||
|
util.printArea(new Triangle(3, 4)); |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -0,0 +1,14 @@ |
|||||
|
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,3 @@ |
|||||
|
通过AI先编写了完整程序,并要求AI一步步讲解代码。随后自行操作,通过AI查找错误点,并运用AI绘制类图并讲解意义。 |
||||
|
继承:子类和父辈强绑定 |
||||
|
组合:类之间依赖松散 |
||||
Loading…
Reference in new issue