JAVA 抽象类运用

来源:互联网 发布:网络教育深圳招生 编辑:程序博客网 时间:2024/06/01 20:18
abstract class Shapeok{
abstract double area();}


class Circle extends Shapeok{


public float r;



Circle(float r)
{
this.r=r;
}

double area()
{
return 3.14*r*r;
}
}





 class Rectang extends Shapeok{


public float width,height;

Rectang(float width,float height) {
// TODO Auto-generated constructor stub


this.width=width;
this.height=height;
}

double area()
{
return width*height;
}
}




public class Shape {


/**
* @param args
*/
public static void main(
String[] args) {
// TODO Auto-generated method stub


double areatotal=0;


Shapeok[] shapes=new Shapeok[2];
shapes[0]=new Circle(1);
shapes[1]=new Rectang(1, 2);
for (int i = 0; i < shapes.length; i++) {
areatotal+=shapes[i].area();
System.out.print(areatotal);
}



}


}