zoukankan      html  css  js  c++  java
  • 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
Copyright © 2011-2022 走看看