5 changed files with 106 additions and 0 deletions
@ -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; |
|||
} |
|||
} |
|||
@ -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("========== 测试完成 =========="); |
|||
} |
|||
} |
|||
@ -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; |
|||
} |
|||
} |
|||
@ -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(); |
|||
} |
|||
@ -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("----------------------"); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue