5 changed files with 122 additions and 0 deletions
@ -0,0 +1,25 @@ |
|||||
|
// 圆形类,继承抽象类Shape
|
||||
|
public class Circle extends Shape { |
||||
|
// 半径
|
||||
|
private double radius; |
||||
|
|
||||
|
// 构造方法:初始化半径
|
||||
|
public Circle(double radius) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
|
||||
|
// 重写getArea方法:圆的面积 = π * r²
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return Math.PI * radius * radius; |
||||
|
} |
||||
|
|
||||
|
// 可选:getter/setter方法(方便后续扩展)
|
||||
|
public double getRadius() { |
||||
|
return radius; |
||||
|
} |
||||
|
|
||||
|
public void setRadius(double radius) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
// 矩形类,继承抽象类Shape
|
||||
|
public class Rectangle extends Shape { |
||||
|
// 长、宽
|
||||
|
private double length; |
||||
|
private double width; |
||||
|
|
||||
|
// 构造方法:初始化长和宽
|
||||
|
public Rectangle(double length, double width) { |
||||
|
this.length = length; |
||||
|
this.width = width; |
||||
|
} |
||||
|
|
||||
|
// 重写getArea方法:矩形面积 = 长 * 宽
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return length * width; |
||||
|
} |
||||
|
|
||||
|
// 可选:getter/setter方法
|
||||
|
public double getLength() { |
||||
|
return length; |
||||
|
} |
||||
|
|
||||
|
public void setLength(double length) { |
||||
|
this.length = length; |
||||
|
} |
||||
|
|
||||
|
public double getWidth() { |
||||
|
return width; |
||||
|
} |
||||
|
|
||||
|
public void setWidth(double width) { |
||||
|
this.width = width; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,5 @@ |
|||||
|
// 抽象父类 Shape,定义所有图形的统一行为
|
||||
|
public abstract class Shape { |
||||
|
// 抽象方法:计算面积,由子类具体实现
|
||||
|
public abstract double getArea(); |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
// 工具类:提供统一处理图形的方法
|
||||
|
public class ShapeUtil { |
||||
|
// 统一打印面积的方法:接收任意Shape子类对象
|
||||
|
public static void printArea(Shape shape) { |
||||
|
System.out.println("图形面积为:" + shape.getArea()); |
||||
|
} |
||||
|
|
||||
|
// 测试主方法(可直接运行验证)
|
||||
|
public static void main(String[] args) { |
||||
|
// 测试圆形:半径5
|
||||
|
Shape circle = new Circle(5); |
||||
|
printArea(circle); // 输出约78.54
|
||||
|
|
||||
|
// 测试矩形:长4,宽3
|
||||
|
Shape rectangle = new Rectangle(4, 3); |
||||
|
printArea(rectangle); // 输出12.0
|
||||
|
|
||||
|
// 测试三角形:底6,高4
|
||||
|
Shape triangle = new Triangle(6, 4); |
||||
|
printArea(triangle); // 输出12.0
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
// 三角形类,继承抽象类Shape
|
||||
|
public class Triangle extends Shape { |
||||
|
// 底、高
|
||||
|
private double base; |
||||
|
private double height; |
||||
|
|
||||
|
// 构造方法:初始化底和高
|
||||
|
public Triangle(double base, double height) { |
||||
|
this.base = base; |
||||
|
this.height = height; |
||||
|
} |
||||
|
|
||||
|
// 重写getArea方法:三角形面积 = 底 * 高 / 2
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return base * height / 2; |
||||
|
} |
||||
|
|
||||
|
// 可选:getter/setter方法
|
||||
|
public double getBase() { |
||||
|
return base; |
||||
|
} |
||||
|
|
||||
|
public void setBase(double base) { |
||||
|
this.base = base; |
||||
|
} |
||||
|
|
||||
|
public double getHeight() { |
||||
|
return height; |
||||
|
} |
||||
|
|
||||
|
public void setHeight(double height) { |
||||
|
this.height = height; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue