抽象类继承的使用-计算图形的周长和面积

先写出抽象类

/**
 * 图形类,抽象类
 * @author Administrator
 *
 */
public abstract class Shape {
	//计算图形周长方法
	public abstract double calcRound();
	//计算图形面积方法
	public abstract double calcArea();	
	
}

 再写出下面被继承的子类方法,通过重写父类的方法继承

//圆类,继承图形类
public class Yuan extends Shape{
        //特定属性-半径
	private double r;
	
	public  Yuan(double r){
		this.r = r;
	}
        //周长
        @Override
	public double calcRound(){
		return 2 * Math.PI *r;
	}
        //面积
        @Override
	public double calcArea(){
		return Math.PI * r * r;
	}
		
}

 

//三角形类
public class Sanjiaoxing {

	private double borderA;
	private double borderB;
	private double borderC;//底边
	private double height;//高
	
	public Sanjiaoxing(double borderA, double borderB, double borderC, double height) {
		super();
		this.borderA = borderA;
		this.borderB = borderB;
		this.borderC = borderC;
		this.height = height;
	}
        @Override
	public double calcRound(){
		return borderA + borderB + borderC;
	}
        @Override
	public double calcArea(){
		return borderC * height / 2;
	}
	
}
//矩形类
public class JuXing extends Shape{

	private double width;
	private double height;
	
	public JuXing(double width, double height) {
		super();
		this.width = width;
		this.height = height;
	}

	@Override
	public double calcRound() {
		
		return (width + height) * 2;
	}

	@Override
	public double calcArea() {
		
		return width * height;
	}
	
}
//测试类
public class Test {

	public static void main(String[] args) {
		Sanjiaoxing s = new Sanjiaoxing(5,4,3,4);
		System.out.println(s.calcRound());
		System.out.println(s.calcArea());
		
		Yuan y = new Yuan(5);
		System.out.println(y.calcRound());
		System.out.println(y.calcArea());

		JuXing j = new JuXing(5,10);
		System.out.println(j.calcRound());
		System.out.println(j.calcArea());
	}

}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值