Browse Source

W4-高钰铎-202506050304

main
Gaoyuduo 1 month ago
parent
commit
8c4079384a
  1. BIN
      4.1.txt
  2. 12
      public class Circle extends Shape {.txt
  3. 14
      public class Rectangle extends Shap.txt
  4. 5
      public class ShapeUtil {.txt
  5. 11
      public class Test {.txt

BIN
4.1.txt

Binary file not shown.

12
public class Circle extends Shape {.txt

@ -0,0 +1,12 @@
public class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}

14
public class Rectangle extends Shap.txt

@ -0,0 +1,14 @@
public class Rectangle extends Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
}

5
public class ShapeUtil {.txt

@ -0,0 +1,5 @@
public class ShapeUtil {
public static void printArea(Shape shape) {
System.out.println("图形面积 = " + shape.getArea());
}
}

11
public class Test {.txt

@ -0,0 +1,11 @@
public class Test {
public static void main(String[] args) {
Shape circle = new Circle(2);
Shape rectangle = new Rectangle(3, 4);
Shape triangle = new Triangle(4, 5);
ShapeUtil.printArea(circle);
ShapeUtil.printArea(rectangle);
ShapeUtil.printArea(triangle);
}
}
Loading…
Cancel
Save