commit 098bd83d7b067d537b2bc92ef744fc49ebe7e281 Author: 范馨遥 <3603458499qq.com> Date: Mon Mar 30 21:27:23 2026 +0800 添加图形面积计算器项目到w5文件夹 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4bd97ca --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +# Maven构建产物 +target/ + +# IDE相关文件 +.idea/ +.vscode/ + +# 系统文件 +.DS_Store +Thumbs.db + +# 日志文件 +*.log + +# 临时文件 +*.tmp +*.temp \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..bb60ab4 --- /dev/null +++ b/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + com.example + shape-calculator + 1.0-SNAPSHOT + + 图形面积计算器 + 图形面积计算器重构实验 + + + 17 + 17 + UTF-8 + + + + + org.junit.jupiter + junit-jupiter-api + 5.9.1 + test + + + org.junit.jupiter + junit-jupiter-engine + 5.9.1 + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.10.1 + + 17 + 17 + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M7 + + + + \ No newline at end of file diff --git a/src/main/java/com/example/shape/Circle.java b/src/main/java/com/example/shape/Circle.java new file mode 100644 index 0000000..d29a20c --- /dev/null +++ b/src/main/java/com/example/shape/Circle.java @@ -0,0 +1,50 @@ +package com.example.shape; + +/** + * 圆形类 + * 继承自Shape抽象类,实现getArea()方法 + */ +public class Circle extends Shape { + private double radius; + + /** + * 构造方法 + * @param radius 圆的半径 + */ + public Circle(double radius) { + this.radius = radius; + } + + /** + * 计算圆形面积 + * @return 圆形的面积 + */ + @Override + public double getArea() { + return Math.PI * radius * radius; + } + + /** + * 获取圆的半径 + * @return 圆的半径 + */ + public double getRadius() { + return radius; + } + + /** + * 设置圆的半径 + * @param radius 圆的半径 + */ + public void setRadius(double radius) { + this.radius = radius; + } + + /** + * 绘制圆形 + */ + @Override + public void draw() { + System.out.println("绘制一个半径为 " + radius + " 的圆形"); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/shape/Rectangle.java b/src/main/java/com/example/shape/Rectangle.java new file mode 100644 index 0000000..73766f5 --- /dev/null +++ b/src/main/java/com/example/shape/Rectangle.java @@ -0,0 +1,69 @@ +package com.example.shape; + +/** + * 矩形类 + * 继承自Shape抽象类,实现getArea()方法 + */ +public class Rectangle extends Shape { + private double width; + private double height; + + /** + * 构造方法 + * @param width 矩形的宽度 + * @param height 矩形的高度 + */ + public Rectangle(double width, double height) { + this.width = width; + this.height = height; + } + + /** + * 计算矩形面积 + * @return 矩形的面积 + */ + @Override + public double getArea() { + return width * height; + } + + /** + * 获取矩形的宽度 + * @return 矩形的宽度 + */ + public double getWidth() { + return width; + } + + /** + * 设置矩形的宽度 + * @param width 矩形的宽度 + */ + public void setWidth(double width) { + this.width = width; + } + + /** + * 获取矩形的高度 + * @return 矩形的高度 + */ + public double getHeight() { + return height; + } + + /** + * 设置矩形的高度 + * @param height 矩形的高度 + */ + public void setHeight(double height) { + this.height = height; + } + + /** + * 绘制矩形 + */ + @Override + public void draw() { + System.out.println("绘制一个宽为 " + width + ",高为 " + height + " 的矩形"); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/shape/Shape.java b/src/main/java/com/example/shape/Shape.java new file mode 100644 index 0000000..004ab8b --- /dev/null +++ b/src/main/java/com/example/shape/Shape.java @@ -0,0 +1,19 @@ +package com.example.shape; + +/** + * 图形抽象类 + * 包含抽象方法getArea(),用于计算图形面积 + * 包含抽象方法draw(),用于绘制图形 + */ +public abstract class Shape { + /** + * 计算图形面积 + * @return 图形的面积 + */ + public abstract double getArea(); + + /** + * 绘制图形 + */ + public abstract void draw(); +} \ No newline at end of file diff --git a/src/main/java/com/example/shape/ShapeCalculatorTest.java b/src/main/java/com/example/shape/ShapeCalculatorTest.java new file mode 100644 index 0000000..1f478bc --- /dev/null +++ b/src/main/java/com/example/shape/ShapeCalculatorTest.java @@ -0,0 +1,26 @@ +package com.example.shape; + +/** + * 图形面积计算器测试类 + */ +public class ShapeCalculatorTest { + public static void main(String[] args) { + // 创建圆形对象,半径为5 + Circle circle = new Circle(5); + System.out.println("圆形的半径: " + circle.getRadius()); + ShapeUtil.printArea(circle); + ShapeUtil.ranShape(circle); + + // 创建矩形对象,宽为4,高为6 + Rectangle rectangle = new Rectangle(4, 6); + System.out.println("矩形的宽: " + rectangle.getWidth() + ", 高: " + rectangle.getHeight()); + ShapeUtil.printArea(rectangle); + ShapeUtil.ranShape(rectangle); + + // 创建三角形对象,底为8,高为3 + Triangle triangle = new Triangle(8, 3); + System.out.println("三角形的底: " + triangle.getBase() + ", 高: " + triangle.getHeight()); + ShapeUtil.printArea(triangle); + ShapeUtil.ranShape(triangle); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/shape/ShapeUtil.java b/src/main/java/com/example/shape/ShapeUtil.java new file mode 100644 index 0000000..7a1b64b --- /dev/null +++ b/src/main/java/com/example/shape/ShapeUtil.java @@ -0,0 +1,24 @@ +package com.example.shape; + +/** + * 图形工具类 + * 提供打印图形面积的方法和绘制图形的方法 + */ +public class ShapeUtil { + /** + * 打印图形的面积 + * @param shape 图形对象 + */ + public static void printArea(Shape shape) { + double area = shape.getArea(); + System.out.println("图形的面积为: " + area); + } + + /** + * 绘制图形 + * @param s 图形对象 + */ + public static void ranShape(Shape s) { + s.draw(); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/shape/Triangle.java b/src/main/java/com/example/shape/Triangle.java new file mode 100644 index 0000000..371a028 --- /dev/null +++ b/src/main/java/com/example/shape/Triangle.java @@ -0,0 +1,69 @@ +package com.example.shape; + +/** + * 三角形类 + * 继承自Shape抽象类,实现getArea()方法 + */ +public class Triangle extends Shape { + private double base; + private double height; + + /** + * 构造方法 + * @param base 三角形的底 + * @param height 三角形的高 + */ + public Triangle(double base, double height) { + this.base = base; + this.height = height; + } + + /** + * 计算三角形面积 + * @return 三角形的面积 + */ + @Override + public double getArea() { + return base * height / 2; + } + + /** + * 获取三角形的底 + * @return 三角形的底 + */ + public double getBase() { + return base; + } + + /** + * 设置三角形的底 + * @param base 三角形的底 + */ + public void setBase(double base) { + this.base = base; + } + + /** + * 获取三角形的高 + * @return 三角形的高 + */ + public double getHeight() { + return height; + } + + /** + * 设置三角形的高 + * @param height 三角形的高 + */ + public void setHeight(double height) { + this.height = height; + } + + /** + * 绘制三角形 + */ + @Override + public void draw() { + System.out.println("绘制一个底为 " + base + ",高为 " + height + " 的三角形"); + } +} \ No newline at end of file