Browse Source

上传文件至 'w4作业'

main
peisishuang 3 months ago
parent
commit
e29387e69a
  1. 8
      w4作业/W4-AI协助记录.txt
  2. 61
      w4作业/W4图形面积计算器重构.ini

8
w4作业/W4-AI协助记录.txt

@ -0,0 +1,8 @@
AI协助记录
1.协助提供统一处理图形的方法:class ShapeUtil
2.修改部分语法错误
3.针对于UML类图实线和虚线疑问的区别进行解释
组合VS继承
1.继承是is关系,组合是has关系
2.继承是父类和子类的关系,它们具有共同特征;而组合是所属关系。例如:电脑是一种电器,是继承关系,用extend;电脑拥有显示屏,是组合关系,用Screen screen。

61
w4作业/W4图形面积计算器重构.ini

@ -0,0 +1,61 @@
W4 图形面积计算器重构
public abstract class Shape {
public abstract double getArea();
}
// 圆形类,继承自 Shape
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
// 矩形类,继承自 Shape
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;
}
}
// 三角形类,继承自 Shape
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;
}
}
// 图形工具类,提供统一处理图形的方法
class ShapeUtil {
// 打印图形面积的方法,接受任意 Shape 子类对象
public static void printArea(Shape shape) {
System.out.println("图形面积: " + shape.getArea());
}
public static void main(String[] args) {
// 创建各种图形对象
Shape circle = new Circle(5.0);
Shape rectangle = new Rectangle(4.0, 6.0);
Shape triangle = new Triangle(3.0, 4.0);
// 统一打印各图形面积
System.out.println("圆形:");
printArea(circle);
System.out.println("矩形:");
printArea(rectangle);
System.out.println("三角形:");
printArea(triangle);
}
}
Loading…
Cancel
Save