5 changed files with 51 additions and 0 deletions
@ -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; |
||||
|
} |
||||
|
} |
||||
@ -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;} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
package shiyan2; |
||||
|
|
||||
|
|
||||
|
public abstract class Shape { |
||||
|
public abstract double getArea(); |
||||
|
} |
||||
|
|
||||
@ -0,0 +1,8 @@ |
|||||
|
package shiyan2; |
||||
|
|
||||
|
// ShapeUtil.java
|
||||
|
public class ShapeUtil { |
||||
|
public static void printArea(Shape shape) { |
||||
|
System.out.println("图形的面积为:" + shape.getArea()); |
||||
|
} |
||||
|
} |
||||
@ -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); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue