6 changed files with 59 additions and 0 deletions
@ -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; |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
public class Main { |
|||
public static void main(String[] args) { |
|||
Shape circle = new Circle(5); |
|||
Shape rectangle = new Rectangle(4, 6); |
|||
Shape triangle = new Triangle(3, 4, 5); |
|||
|
|||
ShapeUtil.printArea(circle); |
|||
ShapeUtil.printArea(rectangle); |
|||
ShapeUtil.printArea(triangle); |
|||
} |
|||
} |
|||
@ -0,0 +1,13 @@ |
|||
public class Rectangle extends Shape { |
|||
private double width, height; |
|||
|
|||
public Rectangle(double width, double height) { |
|||
this.width = width; |
|||
this.height = height; |
|||
} |
|||
|
|||
@Override |
|||
public double getArea() { |
|||
return width * height; |
|||
} |
|||
} |
|||
@ -0,0 +1,3 @@ |
|||
public abstract class Shape { |
|||
public abstract double getArea(); |
|||
} |
|||
@ -0,0 +1,5 @@ |
|||
public class ShapeUtil { |
|||
public static void printArea(Shape shape) { |
|||
System.out.println("面积为: " + shape.getArea()); |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
public class Triangle extends Shape { |
|||
private double a, b, c; |
|||
|
|||
public Triangle(double a, double b, double c) { |
|||
this.a = a; |
|||
this.b = b; |
|||
this.c = c; |
|||
} |
|||
|
|||
@Override |
|||
public double getArea() { |
|||
double s = (a + b + c) / 2; |
|||
return Math.sqrt(s * (s - a) * (s - b) * (s - c)); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue