package javaapplication4;
public class Rect {
protected int length;/////这个地方不能变成私有属性,因为后面继承的类也需要继承它。
protected int width;
public Rect(int length,int width)
{
this.length=length;
this.width=width;
}
public void setLength(int length)
{this.length=length;<br> }
public void setWidth(int width)
{this.width=width;<br> }
public int getLength()
{return length;<br> }
public int getWidth()
{return width;<br> }
public int getArea()
{return length*width;<br> }
public int getCycle()
{return (length+width)*2;<br> }}
////////////////////////////////////////////////////////////////////////////////////////////////////////
package javaapplication4;
public class PlainRect extends Rect
{///此类并未继承父类的长宽属性,所以父类的设计是不合理的。应将其属性改为protected.
protected int startX,startY;
public PlainRect(int length,int width,int startx,int starty)
{
super(length,width);
startX=startx;
startY=starty;
}
public PlainRect()
{
super(0,0);
startX=0;
startY=0;
}
public boolean isInside(double x,double y)
{
boolean b=false;
if(x>startX&&x<startX+length)
if(y>startY&&y<startY+width)
b=true;
return b; }}
////////////////////////////////////////////////////////////////////////////////////////////////////////
package javaapplication4;
public class Main {
public static void main(String[] args) {
PlainRect Pr1=new PlainRect(20,10,10,10);
System.out.println("面积为:"+Pr1.getArea());
System.out.println("周长为:"+Pr1.getCycle());
System.out.println("点(25.5,13)"+(Pr1.isInside(25.5, 13)?"在":"不在")+"此方形内");
} }
温馨提示:内容为网友见解,仅供参考