5 changed files with 115 additions and 0 deletions
@ -0,0 +1,23 @@ |
|||||
|
package w4; |
||||
|
|
||||
|
public class Circle extends Shape { |
||||
|
private double radius; |
||||
|
public Circle() {} |
||||
|
|
||||
|
public Circle(double radius) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return Math.PI * radius * radius; |
||||
|
} |
||||
|
|
||||
|
public double getRadius() { |
||||
|
return radius; |
||||
|
} |
||||
|
|
||||
|
public void setRadius(double radius) { |
||||
|
this.radius = radius; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
package w4; |
||||
|
|
||||
|
public class Rectangle extends Shape { |
||||
|
private double length; |
||||
|
private double width; |
||||
|
|
||||
|
public Rectangle() {} |
||||
|
|
||||
|
public Rectangle(double length, double width) { |
||||
|
this.length = length; |
||||
|
this.width = width; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return length * width; |
||||
|
} |
||||
|
|
||||
|
public double getLength() { |
||||
|
return length; |
||||
|
} |
||||
|
|
||||
|
public void setLength(double length) { |
||||
|
this.length = length; |
||||
|
} |
||||
|
|
||||
|
public double getWidth() { |
||||
|
return width; |
||||
|
} |
||||
|
|
||||
|
public void setWidth(double width) { |
||||
|
this.width = width; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,6 @@ |
|||||
|
package w4; |
||||
|
|
||||
|
public abstract class Shape { |
||||
|
|
||||
|
public abstract double getArea(); |
||||
|
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
package w4; |
||||
|
|
||||
|
public class ShapeUtil { |
||||
|
public static void printArea(Shape shape) { |
||||
|
System.out.println("该图形的面积为:" + shape.getArea()); |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
Shape circle = new Circle(5); |
||||
|
printArea(circle); |
||||
|
|
||||
|
Shape rectangle = new Rectangle(4, 6); |
||||
|
printArea(rectangle); |
||||
|
|
||||
|
Shape triangle = new Triangle(3, 8); |
||||
|
printArea(triangle); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
package w4; |
||||
|
|
||||
|
public class Triangle extends Shape { |
||||
|
private double base; |
||||
|
private double height; |
||||
|
|
||||
|
public Triangle() {} |
||||
|
|
||||
|
public Triangle(double base, double height) { |
||||
|
this.base = base; |
||||
|
this.height = height; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public double getArea() { |
||||
|
return (base * height) / 2; |
||||
|
} |
||||
|
|
||||
|
public double getBase() { |
||||
|
return base; |
||||
|
} |
||||
|
|
||||
|
public void setBase(double base) { |
||||
|
this.base = base; |
||||
|
} |
||||
|
|
||||
|
public double getHeight() { |
||||
|
return height; |
||||
|
} |
||||
|
|
||||
|
public void setHeight(double height) { |
||||
|
this.height = height; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue