以下为完整代码
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;
}
}