75 lines
1.7 KiB
Java
75 lines
1.7 KiB
Java
package exp4;
|
||
|
||
abstract class Shapes {
|
||
public int x,y; //x,y为的坐标
|
||
public int width,height;
|
||
public Shapes(int x,int y,int width,int height) {
|
||
this.x = x;
|
||
this.y = y;
|
||
this.width = width;
|
||
this.height = height;
|
||
}
|
||
public abstract double getArea();
|
||
public abstract double getPerimeter();
|
||
public abstract void show();
|
||
}
|
||
|
||
class Rectangle extends Shapes{
|
||
double a;
|
||
double b;
|
||
|
||
Rectangle(double a, double b){
|
||
super(0, 0, 0, 0);
|
||
this.a=a;
|
||
this.b=b;
|
||
}
|
||
|
||
public double getArea(){
|
||
return a*b;
|
||
}
|
||
|
||
public double getPerimeter(){
|
||
return (a+b)*2;
|
||
}
|
||
|
||
public void show(){
|
||
System.out.println("矩形 长:"+this.a+"宽:"+this.b);
|
||
}
|
||
}
|
||
|
||
class Circle extends Shapes{
|
||
double radius;//半径
|
||
|
||
Circle(double radius){
|
||
super(0, 0, 0, 0);
|
||
this.radius=radius;
|
||
}
|
||
|
||
public double getArea(){
|
||
return 3.14159265324*radius*radius;
|
||
}
|
||
|
||
public double getPerimeter(){
|
||
return 3.14159265324*radius*2;
|
||
}
|
||
|
||
public void show(){
|
||
System.out.println("圆 半径:"+this.radius);
|
||
}
|
||
}
|
||
|
||
|
||
public class ShapeDemo {
|
||
public static void main(String[] args){
|
||
Shapes r = new Rectangle(2, 3);
|
||
Shapes c = new Circle(5);
|
||
|
||
r.show();
|
||
System.out.println("面积:"+r.getArea()+" 周长:"+r.getPerimeter()+"\n");
|
||
|
||
c.show();
|
||
System.out.println("面积:"+c.getArea()+" 周长:"+c.getPerimeter()+"\n");
|
||
|
||
}
|
||
}
|