package com.hanqi.test; public class Rect { private double width=10; private double height=10; 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 2*(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(0,0); } /*** * 判断点是否在矩形内部 * * @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(20); rc.setWidth(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.setHeight(20); pr.setWidth(10); pr.setStartX(10); pr.setStartY(10); pr.isInside(20, -9); //有参构造方法测试 PlainRect pr1=new PlainRect(10, 10, 10, 20); pr1.isInside(25.5, 13); } }