diff --git a/w4/Circle.java b/w4/Circle.java new file mode 100644 index 0000000..99982ad --- /dev/null +++ b/w4/Circle.java @@ -0,0 +1,12 @@ +package shiyan2; + +public class Circle extends Shape { + private double radius; + + public Circle(double radius) { + this.radius = radius; + } + public double getArea() { + return Math.PI * radius * radius; + } +} diff --git a/w4/Rectangle.java b/w4/Rectangle.java new file mode 100644 index 0000000..5f78631 --- /dev/null +++ b/w4/Rectangle.java @@ -0,0 +1,10 @@ +package shiyan2; + +public class Rectangle extends Shape { + private double width, height; + public Rectangle(double width, double height) { + this.width = width; + this.height = height; + } + public double getArea() {return width*height;} +} diff --git a/w4/Shape.java b/w4/Shape.java new file mode 100644 index 0000000..6e98c1a --- /dev/null +++ b/w4/Shape.java @@ -0,0 +1,7 @@ +package shiyan2; + + +public abstract class Shape { + public abstract double getArea(); +} + diff --git a/w4/ShapeUtil.java b/w4/ShapeUtil.java new file mode 100644 index 0000000..85296d6 --- /dev/null +++ b/w4/ShapeUtil.java @@ -0,0 +1,8 @@ +package shiyan2; + +// ShapeUtil.java +public class ShapeUtil { + public static void printArea(Shape shape) { + System.out.println("图形的面积为:" + shape.getArea()); + } +} \ No newline at end of file diff --git a/w4/Test.java b/w4/Test.java new file mode 100644 index 0000000..3926d2e --- /dev/null +++ b/w4/Test.java @@ -0,0 +1,14 @@ +package shiyan2; + +// Test.java +public class Test { + public static void main(String[] args) { + Shape circle = new Circle(3); + Shape rectangle = new Rectangle(4, 5); + Shape triangle = new Triangle(6, 7); + + ShapeUtil.printArea(circle); + ShapeUtil.printArea(rectangle); + ShapeUtil.printArea(triangle); + } +} \ No newline at end of file