6.根据主类,设计类Shape, Rectangle和Circle。

6.根据主类,设计类Shape, Rectangle和Circle。

class A {

   public static void main(String args []) {

        Rectangle r = new Rectangle(0.6f, 1.2f);

       Shape c = new Circle();

        System.out.println("高为1.2,宽为0.6的矩形面积为:" + r.area());

        System.out.println("高为1.2,宽为0.6的矩形周长为:" +r.circumference()); 

        System.out.println("半径为3.5的圆的面积为:" + c.area(3.5f));

        System.out.println("半径为3.5的圆的周长为:" + c.circumference(3.5f));

   }

}


class Shape{
	double h,w;		
	double area(double r){
		return Math.PI*r*r;
	}
	double circumference(double r){
		return 2*Math.PI*r;
	}
}

class Circle extends Shape{
	Circle(){
	}	
}

class Rectangle{
	double h,w;
	Rectangle(double h, double w){
		this.h = h;
		this.w = w;	
	}
	double area(){
		return h*w;
	}
	double circumference(){
		return 2*h+2*w;	
	
	}
}


你可能感兴趣的:(java)