From 7e0facafec5e45dbfdf1fba12ce7a5bf029a80b6 Mon Sep 17 00:00:00 2001 From: JiangYouhan <3080587852@qq.com> Date: Sat, 28 Mar 2026 13:29:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'w4'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w4/Circle.java | 24 ++++++++++++++++++++++++ w4/Main.java | 22 ++++++++++++++++++++++ w4/Rectangle.java | 34 ++++++++++++++++++++++++++++++++++ w4/Shape.java | 16 ++++++++++++++++ w4/ShapeUtil.java | 10 ++++++++++ 5 files changed, 106 insertions(+) create mode 100644 w4/Circle.java create mode 100644 w4/Main.java create mode 100644 w4/Rectangle.java create mode 100644 w4/Shape.java create mode 100644 w4/ShapeUtil.java 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("----------------------"); + } +}