diff --git a/w4/Circle.java b/w4/Circle.java new file mode 100644 index 0000000..953ad52 --- /dev/null +++ b/w4/Circle.java @@ -0,0 +1,24 @@ +package com.rental.shape; + +public class Circle extends Shape { + + private double radius; + + public Circle(double radius) { + super("圆"); + 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; + } +} diff --git a/w4/Main.java b/w4/Main.java new file mode 100644 index 0000000..2c621b5 --- /dev/null +++ b/w4/Main.java @@ -0,0 +1,22 @@ +package com.rental.shape; + +public class Main { + + public static void main(String[] args) { + System.out.println("========== 图形面积计算器测试 ==========\n"); + + Circle circle = new Circle(5); + System.out.println("--- 圆形测试 ---"); + ShapeUtil.printArea(circle); + + Rectangle rectangle = new Rectangle(4, 6); + System.out.println("--- 矩形测试 ---"); + ShapeUtil.printArea(rectangle); + + Triangle triangle = new Triangle(8, 5); + System.out.println("--- 三角形测试 ---"); + ShapeUtil.printArea(triangle); + + System.out.println("========== 测试完成 =========="); + } +} diff --git a/w4/Rectangle.java b/w4/Rectangle.java new file mode 100644 index 0000000..71f754f --- /dev/null +++ b/w4/Rectangle.java @@ -0,0 +1,34 @@ +package com.rental.shape; + +public class Rectangle extends Shape { + + private double width; + private double height; + + public Rectangle(double width, double height) { + super("矩形"); + this.width = width; + this.height = height; + } + + public double getWidth() { + return width; + } + + public void setWidth(double width) { + this.width = width; + } + + public double getHeight() { + return height; + } + + public void setHeight(double height) { + this.height = height; + } + + @Override + public double getArea() { + return width * height; + } +} diff --git a/w4/Shape.java b/w4/Shape.java new file mode 100644 index 0000000..cfb8fdb --- /dev/null +++ b/w4/Shape.java @@ -0,0 +1,16 @@ +package com.rental.shape; + +public abstract class Shape { + + private String name; + + public Shape(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public abstract double getArea(); +} diff --git a/w4/ShapeUtil.java b/w4/ShapeUtil.java new file mode 100644 index 0000000..b16fff5 --- /dev/null +++ b/w4/ShapeUtil.java @@ -0,0 +1,10 @@ +package com.rental.shape; + +public class ShapeUtil { + + public static void printArea(Shape shape) { + System.out.println("图形名称: " + shape.getName()); + System.out.println("面积: " + shape.getArea()); + System.out.println("----------------------"); + } +}