java《设计原则-里氏替换原则》

package dubbo.wangbiao.project.ThreadAndSocket.designprinciples.lishitihuanyuanze.k0;
//长方形
public class Rectangle {
    private  long height;

    public long getHeight() {
        return height;
    }

    public void setHeight(long height) {
        this.height = height;
    }

    public long getWeight() {
        return weight;
    }

    public void setWeight(long weight) {
        this.weight = weight;
    }

    private  long weight;

}
package dubbo.wangbiao.project.ThreadAndSocket.designprinciples.lishitihuanyuanze.k0;
//正方形
public class Square extends Rectangle {
    private  long llength;
    public long getLlength() {
        return llength;
    }

    public void setLlength(long llength) {
        this.llength = llength;
    }

    @Override
    public long getHeight() {
        return getLlength();
    }

    @Override
    public void setHeight(long height) {
        setLlength(height);
    }

    @Override
    public long getWeight() {
        return getLlength();
    }

    @Override
    public void setWeight(long weight) {
        setLlength(weight);
    }
}
package dubbo.wangbiao.project.ThreadAndSocket.designprinciples.lishitihuanyuanze.k0;


public class Simptest {

    public static  void  resize(Rectangle rectangle){
        while(rectangle.getWeight()>=rectangle.getHeight()){
            rectangle.setHeight(rectangle.getHeight()+1);
            System.out.println("高:"+rectangle.getWeight()+"宽;"+rectangle.getHeight());
        }
        System.out.println("最后》高:"+rectangle.getWeight()+"宽;"+rectangle.getHeight());

    }

    public static void main(String[] args) {
//        Rectangle0 rectangle=new Rectangle0();
//        rectangle.setHeight(10);
//        rectangle.setWeight(20);
//        resize(rectangle);
        Square square=new Square();
        square.setLlength(10);
        resize(square);
    }
}
子类可以实现父类的抽象方法,
但是不能覆盖父类的方法
一点点学习,一丝丝进步。不懈怠,才不会被时代淘汰
原文地址:https://www.cnblogs.com/wangbiaohistory/p/14679004.html