抽象類
public abstract class Shape{
? ? public abstract double getArea();
? ? public abstract double getPerimeter();
}
Circle類
public class circle extends Shape{
? ? private double r ;
? ? public Circle(doublic r){
? ? ? ? this.r = r;
????}
? ? public double getArea(){
? ? ? ? double m = Math.PI*r*r;
? ? ? ? return m;
????}
? ? public double getPerimeter(){
? ? ? ? double m = 2*Math.PI*r;
? ? ? ? return m;
????}
}
Rect類
public class Rect extends Shape{
? ? private double a;
? ? private double b;
? ? public Rect(double a,double b){
? ? ? ? this.a = a;
? ? ? ? this.b = b;
????}
? ? public double getArea(){
? ? ? ? double? m = a*b;
? ? ? ? return m;
????}
? ? public double getPerimeter(){
? ? ? ? double m = 2*(a+b);
? ? ? ? return m;
????}
}
測試類
public static void main(String[] args){
? ? Rect r = new Rect(5,4);
? ? double a = r.getArea();
? ? double p = r.getPerimeter();
? ? System.out.println(a);
? ? System.out.println(p);
}?