java shape_java抽象类:[1]抽象类shape

以下为完整代码

public class demo3 {

public static void main(String[] args) {

rectang rec=new rectang(3,5);

System.out.println("Area for Circle with width=3 and height=5 is:"+rec.getArea());

circle cir=new circle(2);

System.out.println("Area for Circle with r=2 is:"+cir.getArea());

}

}

abstract class shape{

public abstract double getArea();

}

class rectang extends shape{

private double width;

private double height;

public rectang(double width,double height){

this.width=width;

this.height=height;

}

public double getArea(){

return width*height;

}

}

class circle extends shape{

private double r;

public circle(double radius){

this.r=radius;

}

public double getArea(){

return Math.PI*r*r;

}

}


版权声明:本文为weixin_39685674原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。