1 changed files with 57 additions and 0 deletions
@ -0,0 +1,57 @@ |
|||
// 抽象图形类 |
|||
abstract class Shape { |
|||
public abstract double getArea(); |
|||
} |
|||
|
|||
// 圆形 |
|||
class Circle extends Shape { |
|||
private double r; |
|||
|
|||
public Circle(double r) { |
|||
this.r = r; |
|||
} |
|||
|
|||
@Override |
|||
public double getArea() { |
|||
return Math.PI * r * r; |
|||
} |
|||
} |
|||
|
|||
// 矩形 |
|||
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; |
|||
} |
|||
} |
|||
|
|||
// 三角形 |
|||
class Triangle extends Shape { |
|||
private double base; |
|||
private double height; |
|||
|
|||
public Triangle(double base, double height) { |
|||
this.base = base; |
|||
this.height = height; |
|||
} |
|||
|
|||
@Override |
|||
public double getArea() { |
|||
return 0.5 * base * height; |
|||
} |
|||
} |
|||
|
|||
// 工具类 |
|||
class ShapeUtil { |
|||
public static void printArea(Shape shape) { |
|||
System.out.println("面积:" + shape.getArea()); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue