package day_15;
/**
* 定義一個平面圖形抽象類
*/
public abstract class PlaneFigure {
public abstract double getArea(); //定義得到面積的抽象方法
public abstract double getPerimeter(); //定義得到周長的抽象方法
}
package day_15;
//定義一個矩形類
public class Rectangle extends PlaneFigure {
private double wide;
private double length;
public Rectangle(){
}
public Rectangle(double wide, double length) {
this.wide = wide;
this.length = length;
}
public double getArea(){ //重寫得到面積的抽象方法
return wide*length;
}
public double getPerimeter(){ //重寫得到周長的抽象方法
return 2*(wide+length);
}
}
package day_15;
//定義一個圓類
public class Circle extends PlaneFigure{
private double r;
private static final double PI=3.14;
public Circle(){
}
public Circle(double r) {
this.r = r;
}
@Override
public double getArea() { //
return PI*r*r;
}
@Override
public double getPerimeter() {
return 2*PI*r;
}
}
package day_15;
public class Test {
public static void main(String[] args) {
Circle circle=new Circle(3);
print(circle);
Rectangle rectangle=new Rectangle(3,4);
print(rectangle);
}
public static void print(PlaneFigure planeFigure){ //定義靜態方法
System.out.println("當前平面圖形的信息:");
System.out.println("\t面積:"+planeFigure.getArea());
System.out.println("\t周長:"+planeFigure.getPerimeter());
}
}