JAVA编写Rect类,按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用...

package com.hanqi.test;

public class Rect {

public double width;

public double height;

//创建有参构造方法

Rect(double width,double height)

{

this.width=width;

this.height=height;

}

//创建无参构造方法

Rect()

{

}

//构建成员方法,获取周长和面积

public double getZhouchang()

{

return 2*(width+height);

}

public double getArea()

{

return width*height;

}

}

package com.hanqi.test;

public class PlainRect extends Rect {

//定义属性

public double startX;

public double startY;

//构建有参数的构造方法

PlainRect(double startX,double startY,double width,double height)

{

this.startX=startX;

this.startY=startY;

this.width=width;

this.height=height;

}

//构建无参构造方法

PlainRect()

{

}

//成员方法,符合条件返回true,不符合条件的返回false。

public boolean isInside(double x,double y)

{

if(x>=startX&&x<=(startX+width)&&y=(startY-height))

{

System.out.println("该点在矩形内部");

return true;

}

else

{

System.out.println("该点不在矩形内部");

return false;

}

}

}

package com.hanqi.test;

public class Test2 {

public static void main(String[] args) {

//无参构造方法求周长

Rect rc=new Rect();

rc.height=20;

rc.width=10;

System.out.println("无参构造方法求:周长是:"+rc.getZhouchang()+",面积是:"+rc.getArea());

//有参构造方法测试

Rect rc1=new Rect(20, 10);

System.out.println("有参构造方法求:周长是;"+rc1.getZhouchang()+"面积是;"+rc1.getArea());

//无参构造方法测试

PlainRect pr=new PlainRect();

pr.height=20;

pr.width=10;

pr.startX=10;

pr.startY=10;

pr.isInside(25.5, 13);

//有参构造方法测试

PlainRect pr1=new PlainRect(10, 10, 10, 20);

pr.isInside(20, -10);

}

}

1465acf8443b27df1d6669eab114e809.png

时间: 09-18

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值