5 changed files with 136 additions and 0 deletions
@ -0,0 +1,27 @@ |
|||||
|
package com.shape; |
||||
|
|
||||
|
public class Circle extends Shape { |
||||
|
private double radius; // 半径
|
||||
|
|
||||
|
public Circle(double radius) { |
||||
|
if (radius <= 0) { |
||||
|
throw new IllegalArgumentException("半径必须大于0"); |
||||
|
} |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
|
||||
|
public double getRadius() { |
||||
|
return radius; |
||||
|
} |
||||
|
|
||||
|
public void setRadius(double radius) { |
||||
|
if (radius > 0) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return Math.PI * radius * radius; // πr²
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
package com.shape; |
||||
|
|
||||
|
public class Rectangle extends Shape { |
||||
|
private double width; // 宽度
|
||||
|
private double height; // 高度
|
||||
|
|
||||
|
public Rectangle(double width, double height) { |
||||
|
if (width <= 0 || height <= 0) { |
||||
|
throw new IllegalArgumentException("长和宽必须大于0"); |
||||
|
} |
||||
|
this.width = width; |
||||
|
this.height = height; |
||||
|
} |
||||
|
|
||||
|
public double getWidth() { |
||||
|
return width; |
||||
|
} |
||||
|
|
||||
|
public void setWidth(double width) { |
||||
|
if (width > 0) this.width = width; |
||||
|
} |
||||
|
|
||||
|
public double getHeight() { |
||||
|
return height; |
||||
|
} |
||||
|
|
||||
|
public void setHeight(double height) { |
||||
|
if (height > 0) this.height = height; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return width * height; // 长 × 宽
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
package com.shape; |
||||
|
|
||||
|
/** |
||||
|
* 抽象类 Shape |
||||
|
* 提供统一的面积计算接口 |
||||
|
*/ |
||||
|
public abstract class Shape { |
||||
|
|
||||
|
/** |
||||
|
* 抽象方法:计算图形面积 |
||||
|
* 子类必须实现该方法 |
||||
|
* @return 图形的面积(double类型) |
||||
|
*/ |
||||
|
public abstract double getArea(); |
||||
|
|
||||
|
/** |
||||
|
* 通用方法:打印图形信息(可选,方便扩展) |
||||
|
*/ |
||||
|
public void display() { |
||||
|
System.out.println(this.getClass().getSimpleName() + " 的面积为: " + getArea()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,23 @@ |
|||||
|
package com.shape; |
||||
|
|
||||
|
/** |
||||
|
* Shape 工具类 |
||||
|
* 利用多态统一处理不同图形 |
||||
|
*/ |
||||
|
public class ShapeUtil { |
||||
|
|
||||
|
/** |
||||
|
* 打印任意 Shape 子类的面积 |
||||
|
* 体现了多态:同一个方法可以接收不同子类对象 |
||||
|
* @param shape 任意继承自 Shape 的图形对象 |
||||
|
*/ |
||||
|
public static void printArea(Shape shape) { |
||||
|
if (shape == null) { |
||||
|
System.out.println("图形对象不能为空!"); |
||||
|
return; |
||||
|
} |
||||
|
System.out.printf("%s 的面积是: %.4f%n", |
||||
|
shape.getClass().getSimpleName(), |
||||
|
shape.getArea()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
package com.shape; |
||||
|
|
||||
|
public class TestShape { |
||||
|
public static void main(String[] args) { |
||||
|
System.out.println("=== 图形面积计算器(使用抽象类重构)===\n"); |
||||
|
|
||||
|
// 创建不同图形对象
|
||||
|
Circle circle = new Circle(5.0); |
||||
|
Rectangle rectangle = new Rectangle(4.0, 6.0); |
||||
|
Triangle triangle = new Triangle(3.0, 4.0, 5.0); |
||||
|
|
||||
|
// 使用 ShapeUtil 统一处理(多态体现)
|
||||
|
ShapeUtil.printArea(circle); |
||||
|
ShapeUtil.printArea(rectangle); |
||||
|
ShapeUtil.printArea(triangle); |
||||
|
|
||||
|
System.out.println("\n=== 使用 display() 方法 ==="); |
||||
|
circle.display(); |
||||
|
rectangle.display(); |
||||
|
triangle.display(); |
||||
|
|
||||
|
// 多态数组示例(进一步展示统一处理优势)
|
||||
|
System.out.println("\n=== 多态数组演示 ==="); |
||||
|
Shape[] shapes = {circle, rectangle, triangle}; |
||||
|
for (Shape s : shapes) { |
||||
|
ShapeUtil.printArea(s); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue