3 changed files with 155 additions and 0 deletions
|
After Width: | Height: | Size: 62 KiB |
@ -0,0 +1,155 @@ |
|||
import java.util.Arrays; |
|||
|
|||
// ========== 1. 抽象类 Shape ==========
|
|||
abstract class Shape { |
|||
protected String name; |
|||
|
|||
public Shape(String name) { |
|||
this.name = name; |
|||
} |
|||
|
|||
public String getName() { |
|||
return name; |
|||
} |
|||
|
|||
public void setName(String name) { |
|||
this.name = name; |
|||
} |
|||
|
|||
public abstract double getArea(); |
|||
|
|||
@Override |
|||
public String toString() { |
|||
return name + " - 面积: " + String.format("%.2f", getArea()); |
|||
} |
|||
} |
|||
|
|||
// ========== 2. 圆形类 ==========
|
|||
class Circle extends Shape { |
|||
private double radius; |
|||
|
|||
public Circle(String name, double radius) { |
|||
super(name); |
|||
this.radius = radius; |
|||
} |
|||
|
|||
public double getRadius() { |
|||
return radius; |
|||
} |
|||
|
|||
public void setRadius(double radius) { |
|||
this.radius = radius; |
|||
} |
|||
|
|||
@Override |
|||
public double getArea() { |
|||
return Math.PI * radius * radius; |
|||
} |
|||
} |
|||
|
|||
// ========== 3. 长方形类 ==========
|
|||
class Rectangle extends Shape { |
|||
private double length; |
|||
private double width; |
|||
|
|||
public Rectangle(String name, double length, double width) { |
|||
super(name); |
|||
this.length = length; |
|||
this.width = width; |
|||
} |
|||
|
|||
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; |
|||
} |
|||
|
|||
@Override |
|||
public double getArea() { |
|||
return length * width; |
|||
} |
|||
} |
|||
|
|||
// ========== 4. 三角形类 ==========
|
|||
class Triangle extends Shape { |
|||
private double base; |
|||
private double height; |
|||
|
|||
public Triangle(String name, double base, double height) { |
|||
super(name); |
|||
this.base = base; |
|||
this.height = height; |
|||
} |
|||
|
|||
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; |
|||
} |
|||
|
|||
@Override |
|||
public double getArea() { |
|||
return (base * height) / 2; |
|||
} |
|||
} |
|||
|
|||
// ========== 5. 工具类 ==========
|
|||
class ShapeUtil { |
|||
|
|||
public static void printArea(Shape shape) { |
|||
if (shape == null) { |
|||
System.out.println("形状不能为空"); |
|||
return; |
|||
} |
|||
System.out.println(shape.toString()); |
|||
} |
|||
|
|||
public static void printAreas(Shape[] shapes) { |
|||
System.out.println("=== 形状面积列表 ==="); |
|||
for (Shape shape : shapes) { |
|||
printArea(shape); |
|||
} |
|||
System.out.println("=================="); |
|||
} |
|||
} |
|||
|
|||
// ========== 6. 主类(必须是public,且与文件名相同)==========
|
|||
public class Main { |
|||
public static void main(String[] args) { |
|||
// 创建各种形状
|
|||
Circle circle = new Circle("圆形", 5.0); |
|||
Rectangle rectangle = new Rectangle("长方形", 4.0, 6.0); |
|||
Triangle triangle = new Triangle("三角形", 3.0, 4.0); |
|||
|
|||
// 使用工具类打印面积
|
|||
System.out.println("单个形状面积打印:"); |
|||
ShapeUtil.printArea(circle); |
|||
ShapeUtil.printArea(rectangle); |
|||
ShapeUtil.printArea(triangle); |
|||
|
|||
// 多态演示:批量处理
|
|||
System.out.println("\n批量处理(多态演示):"); |
|||
Shape[] shapes = {circle, rectangle, triangle}; |
|||
ShapeUtil.printAreas(shapes); |
|||
} |
|||
} |
|||
|
After Width: | Height: | Size: 192 KiB |
Loading…
Reference in new issue