package ooday05;
//求一组图形中的最大面积
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();
double Index =0;
for(int i =1;i<shapes.length;i++){
double arr =shapes[i].area();
if(arr>max){
max =arr;
Index = i;
}
}
System.out.println("最大值="+max+"最大下标="+Index);
}
}
abstract class Shape{//抽象类
protected double c;//受保护的 本类,子类,同包类,周长
public abstract double area();//抽象方法
}
class Square extends Shape{//创造一个正方形的子类继承Shape父类
public Square(int c){
this.c =c;
}
public double area(){//公开的,任何类,重写父类方法
return 0.0625*c*c;
}
}
class Circle extends Shape{
public Circle(int c){
this.c =c ;
}
public double area(){
return 0.0796*c*c;
}
}
逆光之羽
相关分类