其他分享
首页 > 其他分享> > 设计原则 - 里氏替换原则

设计原则 - 里氏替换原则

作者:互联网

目录

概念


编码

实例

/**
 * @Description 长方形
 * @date Dec 15, 2021
 * @Version 1.0
 */
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;
    }
}
/**
 * @Description 正方形
 * @date Dec 15, 2021
 * @Version 1.0
 */
public class Square extends Rectangle{

    /**
     * 边长
     */
    private long sideLength;

    public long getSideLength() {
        return sideLength;
    }

    public void setSideLength(long sideLength) {
        this.sideLength = sideLength;
    }

    @Override
    public long getLength() {
        return getSideLength();
    }

    @Override
    public void setLength(long length) {
        setSideLength(length);
    }

    @Override
    public long getWidth() {
        return getSideLength();
    }

    @Override
    public void setWidth(long width) {
        setSideLength(width);
    }
}
/**
 * @Description 测试类
 * @date Dec 15, 2021
 * @Version 1.0
 */
public class Test {

    /**
     * 调整大小
     * 当宽小于等于长的时候给宽度+1,直到它们相等
     * @param rectangle
     */
    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 end, width: " + rectangle.getWidth() + " length: " + rectangle.getLength());
    }
}
public static void main(String[] args) {
    Rectangle rectangle = new Rectangle();
    rectangle.setWidth(10);
    rectangle.setLength(20);
    resize(rectangle);
}
width: 11 length: 20
width: 12 length: 20
width: 13 length: 20
width: 14 length: 20
width: 15 length: 20
width: 16 length: 20
width: 17 length: 20
width: 18 length: 20
width: 19 length: 20
width: 20 length: 20
width: 21 length: 20
resize end, width: 21 length: 20
/**
 * 正方形(子类)验证
 * @param args
 */
public static void main(String[] args) {
    Square square = new Square();
    square.setLength(10);
    resize(square);
}
width: 11 length: 11
width: 12 length: 12
width: 13 length: 13
width: 14 length: 14
width: 15 length: 15
width: 16 length: 16
width: 17 length: 17
width: 18 length: 18
width: 19 length: 19
width: 20 length: 20
width: 21 length: 21
width: 22 length: 22
width: 23 length: 23
width: 24 length: 24
width: 25 length: 25
width: 26 length: 26
width: 27 length: 27
width: 28 length: 28
....................

里氏替换原则

/**
 * @Description 四边形
 * @date Dec 19, 2021
 * @version 1.0
 */
public interface Quadrangle {

    long getWidth();

    long getLength();

}
/**
 * @Description 长方形
 * @date Dec 15, 2021
 * @Version 1.0
 */
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;
    }
}
/**
 * @Description 正方形
 * @date Dec 15, 2021
 * @Version 1.0
 */
public class Square implements Quadrangle {

    /**
     * 边长
     */
    private long sideLength;

    @Override
    public long getWidth() {
        return sideLength;
    }

    @Override
    public long getLength() {
        return sideLength;
    }

    public long getSideLength() {
        return sideLength;
    }

    public void setSideLength(long sideLength) {
        this.sideLength = sideLength;
    }
}

在这里插入图片描述


总结


源码


- End -
- 个人学习笔记 -

标签:20,原则,里氏,long,替换,width,length,子类,public
来源: https://www.cnblogs.com/maggieq8324/p/15782984.html