模板模式

抽象类

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);

你可能感兴趣的:(模板模式)