diff --git a/w4/Circle.java b/w4/Circle.java new file mode 100644 index 0000000..276c930 --- /dev/null +++ b/w4/Circle.java @@ -0,0 +1,10 @@ +package w4; +public class Circle extends Shape{ + private double R; + public Circle(double R){ + this.R=R; + } + public double getArea(){ + return Math.PI*R*R; + } +} diff --git a/w4/Rectangle.java b/w4/Rectangle.java new file mode 100644 index 0000000..f4d737c --- /dev/null +++ b/w4/Rectangle.java @@ -0,0 +1,12 @@ +package w4; +public class Rectangle extends Shape { + private double wide; + private double high; + public Rectangle(double wide,double high){ + this.wide=wide; + this.high=high; + } + public double getArea(){ + return wide*high; + } +} diff --git a/w4/Shape.java b/w4/Shape.java new file mode 100644 index 0000000..e566598 --- /dev/null +++ b/w4/Shape.java @@ -0,0 +1,7 @@ +package w4; +public abstract class Shape { + public abstract double getArea(); +} + + + diff --git a/w4/ShapeUtil.java b/w4/ShapeUtil.java new file mode 100644 index 0000000..078bc24 --- /dev/null +++ b/w4/ShapeUtil.java @@ -0,0 +1,6 @@ +package w4; +public class ShapeUtil { + public static void printArea(Shape shape){ + System.out.printf("图形面积:%.2f%n",shape.getArea()); + } +} diff --git a/w4/Test.java b/w4/Test.java new file mode 100644 index 0000000..5cb1d65 --- /dev/null +++ b/w4/Test.java @@ -0,0 +1,11 @@ +package w4; +public class Test { + public static void main(String[] args){ + Shape circle=new Circle(5); + Shape rectangle=new Rectangle(5,12); + Shape triangle=new Triangle(2,4); + ShapeUtil.printArea(circle); + ShapeUtil.printArea(rectangle); + ShapeUtil.printArea(triangle); + } +} diff --git a/w4/Triangle.java b/w4/Triangle.java new file mode 100644 index 0000000..ba8e0b9 --- /dev/null +++ b/w4/Triangle.java @@ -0,0 +1,12 @@ +package w4; +public class Triangle extends Shape { + private double base; + private double high; + public Triangle(double base,double high){ + this.base=base; + this.high=high; + } + public double getArea(){ + return base*high*0.5; + } +} diff --git a/w4/类图.png b/w4/类图.png new file mode 100644 index 0000000..eac17ec Binary files /dev/null and b/w4/类图.png differ diff --git a/w4/运行截图.png b/w4/运行截图.png new file mode 100644 index 0000000..25d06d9 Binary files /dev/null and b/w4/运行截图.png differ