//（1）平面图形类，抽象类的定义
package ch06_09;
public abstract class PlaneGraphics1 {
	private String shape; // 形状
	public PlaneGraphics1(String shape){// 构造方法，不能是抽象方法
		this.shape = shape;
	}
	public PlaneGraphics1() {
		this("未知");
	}
	public abstract double area(); // 计算面积，抽象方法
	public void print(){ // 显示面积，非抽象方法
		System.out.println(this.shape + "面积为 " + this.area());
	}
}
//（2）长方形类的实现
package ch06_09;
public class Rectangle1 extends PlaneGraphics1 {
	private double length; // 长度
	private double width;  // 宽度
	public Rectangle1(double length, double width) { // 构造方法
		super("长方形");
		this.length = length;
		this.width = width;
	}
	public Rectangle1(double width){ // 正方形是长方形的特例
		super("正方形");
		this.length = width;
		this.width = width;
	}
	public Rectangle1() {
		this(0, 0);
	}
	public void setLength(double length) {
		this.length = length;
	}
	public void setWidth(double width) {
		this.width = width;
	}
	public double getLength() {
		return this.length;
	}
	public double getWidth() {
		return this.width;
	}
	@Override
    public double area() { //计算长方形面积，实现父类的抽象方法
        return this.width * this.length;
    }
}
//（3）椭圆类的实现
package ch06_09;
public class Ellipse1 extends PlaneGraphics1 {
	private double radius_a;    // a轴半径
	private double radius_b;    // b轴半径
	public Ellipse1(double radius_a, double radius_b) { // 构造方法
		super("椭圆");
		this.radius_a = radius_a;
		this.radius_b = radius_b;
	}
	public Ellipse1(double radius_a) { // 圆是椭圆的特例
		super("圆");
		this.radius_a = radius_a;
		this.radius_b = radius_a;
	}
	public Ellipse1() {
		this(0, 0);
	}
	@Override
	public double area() {// 计算椭圆面积，实现父类的抽象方法
		return Math.PI * this.radius_a * this.radius_b;
	}
}
//（4）测试主类，注意采用的是“向上转型”，从而更便于程序的设计
package ch06_09;
public class TestPlaneGraphics {
	public static void main(String[] args) {
		PlaneGraphics1 p[]=new PlaneGraphics1[4];
		p[0]= new Rectangle1(10,20);    //获得子类对象，长方形
		p[1]= new Rectangle1(10);        //正方形
		p[2]=  new Ellipse1(10,20);      //椭圆
		p[3]=new Ellipse1(10);           //圆
		System.out.println("创建的4个图形信息如下：");
		for(int i=0;i<4;++i){
			p[i].print();
		}
	}
}
