继承求不同形状图形的周长和面积

package LESSON6;
//形状类
class Shape{
    public final double PI=3.14;
    public double s;//面积
    public double c;//周长
    public Shape(double l,double h){
        this.s=l*h;
        this.c=2*(l+h);
        System.out.println("矩形面积:"+s+"周长:"+c);        
    }
    public Shape(double r){
        this.s=PI*r*r;
        this.c=2*PI*r;    
        System.out.println("圆面积:"+s+"周长:"+c);
    }        
}
//矩形类
class Rect extends Shape{
    public Rect(double h){
        super(1.0,1.2);//矩形
        this.s=h*h;
        this.c=4*h;
        System.out.println("正方形"+"面积:"+s+"周长:"+c);                
    }        
}
//圆形类
class Circle extends Shape{
    public Circle(){
        super(3);//矩形                
    }        
}
//正方形类
class Square extends Rect{
    public Square(){
        super(4);        
    }    
}
public class Exercise {
    public static void main(String[] args) {
        Rect re=new Rect(2);
        Circle cir=new Circle();
//        Square sq=new Square();//正方形对象不需创建,在创建矩形对象时已经输出的正方形的周长和面积                
    }
}

 

posted @ 2019-05-29 21:26  勤奋的园  阅读(423)  评论(0编辑  收藏  举报