commit
098bd83d7b
8 changed files with 327 additions and 0 deletions
@ -0,0 +1,17 @@ |
|||
# Maven构建产物 |
|||
target/ |
|||
|
|||
# IDE相关文件 |
|||
.idea/ |
|||
.vscode/ |
|||
|
|||
# 系统文件 |
|||
.DS_Store |
|||
Thumbs.db |
|||
|
|||
# 日志文件 |
|||
*.log |
|||
|
|||
# 临时文件 |
|||
*.tmp |
|||
*.temp |
|||
@ -0,0 +1,53 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" |
|||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|||
<modelVersion>4.0.0</modelVersion> |
|||
|
|||
<groupId>com.example</groupId> |
|||
<artifactId>shape-calculator</artifactId> |
|||
<version>1.0-SNAPSHOT</version> |
|||
|
|||
<name>图形面积计算器</name> |
|||
<description>图形面积计算器重构实验</description> |
|||
|
|||
<properties> |
|||
<maven.compiler.source>17</maven.compiler.source> |
|||
<maven.compiler.target>17</maven.compiler.target> |
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
|||
</properties> |
|||
|
|||
<dependencies> |
|||
<dependency> |
|||
<groupId>org.junit.jupiter</groupId> |
|||
<artifactId>junit-jupiter-api</artifactId> |
|||
<version>5.9.1</version> |
|||
<scope>test</scope> |
|||
</dependency> |
|||
<dependency> |
|||
<groupId>org.junit.jupiter</groupId> |
|||
<artifactId>junit-jupiter-engine</artifactId> |
|||
<version>5.9.1</version> |
|||
<scope>test</scope> |
|||
</dependency> |
|||
</dependencies> |
|||
|
|||
<build> |
|||
<plugins> |
|||
<plugin> |
|||
<groupId>org.apache.maven.plugins</groupId> |
|||
<artifactId>maven-compiler-plugin</artifactId> |
|||
<version>3.10.1</version> |
|||
<configuration> |
|||
<source>17</source> |
|||
<target>17</target> |
|||
</configuration> |
|||
</plugin> |
|||
<plugin> |
|||
<groupId>org.apache.maven.plugins</groupId> |
|||
<artifactId>maven-surefire-plugin</artifactId> |
|||
<version>3.0.0-M7</version> |
|||
</plugin> |
|||
</plugins> |
|||
</build> |
|||
</project> |
|||
@ -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 + " 的圆形"); |
|||
} |
|||
} |
|||
@ -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 + " 的矩形"); |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
package com.example.shape; |
|||
|
|||
/** |
|||
* 图形抽象类 |
|||
* 包含抽象方法getArea(),用于计算图形面积 |
|||
* 包含抽象方法draw(),用于绘制图形 |
|||
*/ |
|||
public abstract class Shape { |
|||
/** |
|||
* 计算图形面积 |
|||
* @return 图形的面积 |
|||
*/ |
|||
public abstract double getArea(); |
|||
|
|||
/** |
|||
* 绘制图形 |
|||
*/ |
|||
public abstract void draw(); |
|||
} |
|||
@ -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); |
|||
} |
|||
} |
|||
@ -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(); |
|||
} |
|||
} |
|||
@ -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 + " 的三角形"); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue