8 changed files with 58 additions and 0 deletions
@ -0,0 +1,10 @@ |
|||||
|
package w4; |
||||
|
public class Circle extends Shape{ |
||||
|
private double R; |
||||
|
public Circle(double R){ |
||||
|
this.R=R; |
||||
|
} |
||||
|
public double getArea(){ |
||||
|
return Math.PI*R*R; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
package w4; |
||||
|
public class Rectangle extends Shape { |
||||
|
private double wide; |
||||
|
private double high; |
||||
|
public Rectangle(double wide,double high){ |
||||
|
this.wide=wide; |
||||
|
this.high=high; |
||||
|
} |
||||
|
public double getArea(){ |
||||
|
return wide*high; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,7 @@ |
|||||
|
package w4; |
||||
|
public abstract class Shape { |
||||
|
public abstract double getArea(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
@ -0,0 +1,6 @@ |
|||||
|
package w4; |
||||
|
public class ShapeUtil { |
||||
|
public static void printArea(Shape shape){ |
||||
|
System.out.printf("图形面积:%.2f%n",shape.getArea()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,11 @@ |
|||||
|
package w4; |
||||
|
public class Test { |
||||
|
public static void main(String[] args){ |
||||
|
Shape circle=new Circle(5); |
||||
|
Shape rectangle=new Rectangle(5,12); |
||||
|
Shape triangle=new Triangle(2,4); |
||||
|
ShapeUtil.printArea(circle); |
||||
|
ShapeUtil.printArea(rectangle); |
||||
|
ShapeUtil.printArea(triangle); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
package w4; |
||||
|
public class Triangle extends Shape { |
||||
|
private double base; |
||||
|
private double high; |
||||
|
public Triangle(double base,double high){ |
||||
|
this.base=base; |
||||
|
this.high=high; |
||||
|
} |
||||
|
public double getArea(){ |
||||
|
return base*high*0.5; |
||||
|
} |
||||
|
} |
||||
|
After Width: | Height: | Size: 36 KiB |
|
After Width: | Height: | Size: 206 KiB |
Loading…
Reference in new issue