5 changed files with 61 additions and 0 deletions
@ -0,0 +1,47 @@ |
|||
package Shape; |
|||
|
|||
abstract public class Shape { |
|||
abstract double getArea(); |
|||
} |
|||
class Circle extends Shape{ |
|||
private double r; |
|||
public Circle(double r){ |
|||
this.r=r; |
|||
} |
|||
@Override |
|||
double getArea(){ |
|||
return Math.PI*r*r; |
|||
} |
|||
} |
|||
|
|||
class Rectangle extends Shape{ |
|||
private double chang; |
|||
private double kuan; |
|||
public Rectangle(double chang,double kuan){ |
|||
this.chang=chang; |
|||
this.kuan=kuan; |
|||
} |
|||
@Override |
|||
double getArea(){ |
|||
return chang*kuan; |
|||
} |
|||
} |
|||
|
|||
class Triangle extends Shape{ |
|||
private double di; |
|||
private double height; |
|||
public Triangle(double di,double height){ |
|||
this.di=di; |
|||
this.height=height; |
|||
} |
|||
@Override |
|||
double getArea(){ |
|||
return 0.5*di*height; |
|||
} |
|||
} |
|||
|
|||
class ShapeUtil{ |
|||
public static void printArea(Shape shape){ |
|||
System.out.println("面积是"+shape.getArea()); |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
package Shape; |
|||
|
|||
public class ShapeTest { |
|||
public static void main(String[] args){ |
|||
System.out.println("--测试三个类--"); |
|||
//circle
|
|||
Circle round=new Circle(5); |
|||
Rectangle b=new Rectangle(4,7); |
|||
Triangle c=new Triangle(8,4); |
|||
ShapeUtil.printArea(round); |
|||
ShapeUtil.printArea(b); |
|||
ShapeUtil.printArea(c); |
|||
} |
|||
} |
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue