public class Rectangle {
private long length;
private long width;
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public long getWidth() {
return width;
}
public void setWidth(long width) {
this.width = width;
}
}
public class Square extends Rectangle {
private long sideLength;
@Override
public long getLength() {
return sideLength;
}
@Override
public void setLength(long length) {
this.sideLength = length;
}
@Override
public long getWidth() {
return sideLength;
}
@Override
public void setWidth(long width) {
this.sideLength = width;
}
}
在测试长方形时,代码不会出现问题
public class Test {
public static void resize(Rectangle rectangle){
while (rectangle.getWidth() <= rectangle.getLength()){
rectangle.setWidth(rectangle.getWidth()+1);
System.out.println("width:"+rectangle.getWidth() + " length:"+rectangle.getLength());
}
System.out.println("resize方法结束 width:"+rectangle.getWidth() + " length:"+rectangle.getLength());
}
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.setWidth(10);
rectangle.setLength(20);
resize(rectangle);
}
}
public abstract class Base {
public abstract Map method();
}
public class Child extends Base {
@Override
public HashMap method() {
HashMap hashMap = new HashMap();
System.out.println("子类method被执行");
hashMap.put("message","子类method被执行");
return hashMap;
}
}
public class Test {
public static void main(String[] args) {
Child child = new Child();
System.out.println(child.method());
}
}