package com.hanqi.test; public class Rect {
private double width=; private double height=; public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//创建有参构造方法
public Rect(double width,double height)
{
this.width=width;
this.height=height;
}
//创建无参构造方法
public Rect()
{ }
//构建成员方法,获取周长和面积
public double getZhouchang()
{
return *(width+height);
}
public double getArea() {
return width*height;
} }
package com.hanqi.test; public class PlainRect extends Rect {
//定义属性
private double startX; private double startY;
public double getStartX() {
return startX;
}
public void setStartX(double startX) {
this.startX = startX;
}
public double getStartY() {
return startY;
}
public void setStartY(double startY) {
this.startY = startY;
}
//构建有参数的构造方法
public PlainRect(double startX,double startY,double width,double height)
{ super(width,height);
this.startX=startX; this.startY=startY; }
//构建无参构造方法
public PlainRect()
{
super(,);
}
/***
* 判断点是否在矩形内部
*
* @param x 点的x坐标
* @param y 点的y坐标
* @return 是否在矩形内部
*/
//成员方法,符合条件返回true,不符合条件的返回false。
public boolean isInside(double x,double y) {
if(x>=startX&&x<=(startX+getWidth())&&y<startY&&y>=(startY-getHeight()))
{
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.setHeight();
rc.setWidth();
System.out.println("无参构造方法求:周长是:"+rc.getZhouchang()+",面积是:"+rc.getArea());
//有参构造方法测试
Rect rc1=new Rect(, );
System.out.println("有参构造方法求:周长是;"+rc1.getZhouchang()+"面积是;"+rc1.getArea());
//无参构造方法测试
PlainRect pr=new PlainRect(); pr.setHeight();
pr.setWidth();
pr.setStartX();
pr.setStartY(); pr.isInside(, -);
//有参构造方法测试
PlainRect pr1=new PlainRect(, , , );
pr1.isInside(25.5, ); } }