Browse Source

上传文件至 'w4'

main
JiangYouhan 3 weeks ago
parent
commit
7e0facafec
  1. 24
      w4/Circle.java
  2. 22
      w4/Main.java
  3. 34
      w4/Rectangle.java
  4. 16
      w4/Shape.java
  5. 10
      w4/ShapeUtil.java

24
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;
}
}

22
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("========== 测试完成 ==========");
}
}

34
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;
}
}

16
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();
}

10
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("----------------------");
}
}
Loading…
Cancel
Save