1 changed files with 55 additions and 0 deletions
@ -0,0 +1,55 @@ |
|||
package com.rental; |
|||
public class ShiYan { |
|||
static abstract class Shape { |
|||
public abstract double getArea(); |
|||
} |
|||
static class Circle extends Shape { |
|||
private double radius; |
|||
|
|||
public Circle(double radius) { |
|||
this.radius = radius; |
|||
} |
|||
@Override |
|||
public double getArea() { |
|||
return Math.PI * radius * radius; |
|||
} |
|||
} |
|||
// 矩形
|
|||
static 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; |
|||
} |
|||
} |
|||
static 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; |
|||
} |
|||
} |
|||
static class ShapeUtil { |
|||
public static void printArea(Shape shape) { |
|||
System.out.println("图形面积为:" + shape.getArea()); |
|||
} |
|||
} |
|||
public static void main(String[] args) { |
|||
Shape circle = new Circle(5); |
|||
Shape rectangle = new Rectangle(4, 6); |
|||
Shape triangle = new Triangle(3, 8); |
|||
ShapeUtil.printArea(circle); |
|||
ShapeUtil.printArea(rectangle); |
|||
ShapeUtil.printArea(triangle); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue