找最大图形面积 (抽象方法)

//最大图形的面积
public class ShapeTest {

    public static void main(String[] args) {
        Shape[] shapes = new Shape[4];
        shapes[0] = new Square(1);
        shapes[1] = new Square(2);
        shapes[2] = new Circle(1);
        shapes[3] = new Circle(2);
        maxArea(shapes);

    }


    public static void maxArea(Shape[] Shapes){
        double max = Shapes[0].area();
        int maxIndex = 0;
        for(int i=1;i<Shapes.length;i++){
            double area = Shapes[i].area();
            if(area>max){
                max=area;
                maxIndex=i;
            }
        }
        System.out.println("最大面积为:"+max+",所在下标为:"+maxIndex);
    }    
}    
    
    
    
    abstract class Shape{
        protected double c;
        public abstract double area();
    }

    
    
    
    class Square extends Shape{
        public Square(double c){
            this.c=c;
        }
        public double area(){
            return 0.0625*c*c; //0.0796
        }
    } 


    
    
    class Circle extends Shape{
        public Circle(double c){
        this.c=c;
    }
        public double area(){
        return 0.0796*c*c;
    }
}
原文地址:https://www.cnblogs.com/luckyBrown/p/5886918.html