首页 > 其他 > 详细

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

时间:2016-09-19 23:53:58      阅读:287      评论:0      收藏:0      [点我收藏+]
//最大图形的面积
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;
    }
}

 

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

原文:http://www.cnblogs.com/luckyBrown/p/5886918.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!