diff --git a/w4/Circle.java b/w4/Circle.java new file mode 100644 index 0000000..584068d --- /dev/null +++ b/w4/Circle.java @@ -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; + } +} \ No newline at end of file diff --git a/w4/Rectangle.java b/w4/Rectangle.java new file mode 100644 index 0000000..90cb267 --- /dev/null +++ b/w4/Rectangle.java @@ -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; + } +} \ No newline at end of file diff --git a/w4/Shape.java b/w4/Shape.java new file mode 100644 index 0000000..e1a96c5 --- /dev/null +++ b/w4/Shape.java @@ -0,0 +1,3 @@ +public abstract class Shape { + public abstract double getArea(); +} \ No newline at end of file diff --git a/w4/ShapeTest.java b/w4/ShapeTest.java new file mode 100644 index 0000000..136a171 --- /dev/null +++ b/w4/ShapeTest.java @@ -0,0 +1,18 @@ +public class ShapeTest { + public static void main(String[] args) { + // 创建各个图形对象 + Shape c = new Circle(5); + Shape r = new Rectangle(4,6); + Shape t = new Triangle(3,4); + + //统一调用工具打印面积 + System.out.print("圆形:"); + ShapeUtil.printArea(c); + + System.out.print("矩形:"); + ShapeUtil.printArea(r); + + System.out.print("三角形:"); + ShapeUtil.printArea(t); + } +} \ No newline at end of file diff --git a/w4/ShapeUtil.java b/w4/ShapeUtil.java new file mode 100644 index 0000000..37e32e3 --- /dev/null +++ b/w4/ShapeUtil.java @@ -0,0 +1,5 @@ +public class ShapeUtil { + public static void printArea(Shape shape){ + System.out.println("图形面积:" + shape.getArea()); + } +} \ No newline at end of file