`

自定义形状类(继承的运用)

    博客分类:
  • J2SE
阅读更多
/**
 * 
 * 自定义图形的基类
 */
public abstract class MyShape 
{
	/**形状的名字*/
	protected String name;
	/*
	 * protected访问控制符表示只有本类和子类能够访问该属性
	 */
	/**抽象方法,获取形状的周长*/
	public abstract double getGrith();
	
	/**抽象方法,获取形状的面积*/
	public abstract double getArea();
	/**抽象方法,输出形状*/
	public abstract String toString();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
}


/**
 * 
 * 三角形
 */
public class Triangle extends MyShape
{

	private double sideA;
	private double sideB;
	private double sideC;
	public static final String SIDEERR = "三角形的边长不能够小于0!";
	public static final String SHAPEERR = "三角形的两边之和必须大于第三边";
	
	public Triangle()
	{
		init();
	}
	
	public Triangle(double a,double b,double c)
	{
		/*
		 * 如果给定的三条边能够组成三角形,便用给定的边长构成三角形
		 */
		
	}
	
	private void init()
	{
		this.sideA = 3;
		this.sideB = 4;
		this.sideC = 5;
	}
	
	private boolean isTrianglelegal(double a,double b,double c)
	{
		//三条边的长度必须大于0
		if((a<=0) || (b<=0) || (c<=0))
		{
			System.out.println(SIDEERR);
			return false;
		}
		else if((a+b<c) || (a+c<b) || (b+c<a))
		{
			//两边之和必须大于第三边
			System.out.println(SHAPEERR);
			return false;
		}
		return true;
	}
	
	public double getArea() {
		double s = (this.sideA + this.sideB + this.sideC) /2;
		
		return Math.sqrt(s*(s - this.sideA) * (s-this.sideB) * (s-this.sideC));
	}

	public double getGrith() {
		return this.sideA + this.sideB + this.sideC;
	}

	public String toString() {
		// TODO Auto-generated method stub
		
		return "三角形的名字是:" + this.name + " ,它的三条边的边长分别是:" 
			+ this.sideA + ", " + this.sideB + ", " + this.sideC;
	}

	public double getSideA() {
		return sideA;
	}

	public void setSideA(double sideA) {
		if(this.isTrianglelegal(sideA, this.sideB, this.sideC))
		{
			this.sideA = sideA;
		}
	}

	public double getSideB() {
		return sideB;
	}

	public void setSideB(double sideB) {
		if(this.isTrianglelegal(this.sideA, sideB, this.sideC))
		{
			this.sideB = sideB;
		}
		
	}

	public double getSideC() {
		return sideC;
	}

	public void setSideC(double sideC)
	{
		if(this.isTrianglelegal(this.sideA, this.sideB, sideC))
		{
			this.sideC = sideC;
		}
	}

	public static void main(String[] args)
	{
		Triangle test = new Triangle();
		test.setName("myTriangle");
		System.out.println(test.toString());
		System.out.println("三角形的周长是:" + test.getGrith());
		System.out.println("三角形的面积是:" + test.getArea());
	}
}

/**
 * 
 * 长方形
 */
public class Rectangle extends MyShape 
{

	private double length;
	private double width;
	public static final String SIDEERR = "长方形的长和宽必须大于0!";
	public Rectangle()
	{
		init();
	}
	
	public Rectangle(double a,double b)
	{
		if((a<=0) || (b<=0))
		{
			System.out.println(SIDEERR);
			init();
		}
		else
		{
			this.length = a;
			this.width = b;
		}
	}
	
	private void init()
	{
		this.length = 5;
		this.width = 4;
	}
	
	public double getArea() {
		return this.length * this.width;
	}

	public double getGrith() {
		
		return (this.length + this.width)/2;
	}

	public String toString() {
		
		return "矩形的名字为:" + this.name + ",长为" + this.length + ",宽为 " + this.width;
	}

	public double getLength() {
		return length;
	}

	public void setLength(double length) {
		if(length>0)
		{
			this.length = length;
		}
		else
		{
			System.out.println(SIDEERR);
		}
	}

	public double getWidth() {
		return width;
	}

	public void setWidth(double width) {
		if(width>0)
		{
			this.width = width;
		}
		else
		{
			System.out.println(SIDEERR);
		}
	}
	
	public static void main(String [] args)
	{
		Rectangle test = new Rectangle();
		test.setName("myRectangle");;
		System.out.println(test.toString());
		System.out.println("矩形的周长是:" + test.getGrith());
		System.out.println("矩形的面积是:" + test.getArea());
	}

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics