CoreJava/exp4/ShapeDemo.java
2024-05-09 09:10:51 +08:00

75 lines
1.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package exp4;
abstract class Shapes {
public int x,y; //xy为的坐标
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");
}
}