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


/**
*
* 自定义图形的基类
*/
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());
}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值