码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-09-19 23:55:15      阅读:155      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!