期末考试第三题


3. (15分) 编写程序,实现图形面积计算功能:
(1) 定义抽象类Shape,包含抽象方法:void printArea();
(2) 定义Shape的派生类Circle,包含一个int型radius字段,用于描述圆形的半径长度;实现printArea方法,计算并输出圆形的面积(PI=3.14);
(3) 定义Shape的派生类Square,包含一个int型sideLength字段,用于描述正方形边长;实现printArea方法,计算并输出正方形的面积;
(4) 定义ShapeTest类,在main方法中使用多态的形式测试上述printArea方法。
 

package project3;
public abstract class Shape {
    public abstract void printArea();
}
class Circle extends Shape{
	private static final float PI=(float) 3.14;
    private int radius;
	@Override
	public void printArea() {
		// TODO Auto-generated method stub
		System.out.println("该圆形的面积是:"+PI*radius*radius);
		
	}
	public Circle(int radius) {
		super();
		this.radius = radius;
	}
	
	
}
class Square extends Shape{
    private int sideLength;
	@Override
	public void printArea() {
		// TODO Auto-generated method stub
		System.out.println("该正方形的面积是:"+sideLength*sideLength);
		
	}
	public Square(int sideLength) {
		super();
		this.sideLength = sideLength;
	}
	
	
}
class ShapeText{
	public static void main(String[] args) {
		Shape a=new Circle(3);
		Shape b=new Square(5);
		a.printArea();
		b.printArea();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值