package tech.selinux.design.principle.liskovsubstitution;
public interface Quadrangle {
long getWidth();
long getLength();
}
package tech.selinux.design.principle.liskovsubstitution;
public class Rectangle implements Quadrangle {
private long length;
private long width;
@Override
public long getWidth() {
return width;
}
@Override
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public void setWidth(long width) {
this.width = width;
}
}
package tech.selinux.design.principle.liskovsubstitution;
public class Square implements Quadrangle {
private long sideLength;
public long getSideLength() {
return sideLength;
}
public void setSideLength(long sideLength) {
this.sideLength = sideLength;
}
@Override
public long getWidth() {
return sideLength;
}
@Override
public long getLength() {
return sideLength;
}
}
package tech.selinux.design.principle.liskovsubstitution;
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 static void main(String[] args) {
Square square = new Square();
// square.setLength(10);
// resize(square);
}
}