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);
        }
    }
    
    子类可以实现父类的抽象方法,
    但是不能覆盖父类的方法
    
    一点点学习,一丝丝进步。不懈怠,才不会被时代淘汰
  • 相关阅读:
    观察者模式
    饿汉单例模式 and 懒汉单例模式
    解决hash冲突之分离链接法
    bat处理文件
    使用json-org包实现POJO和json的转换
    并发修改异常(ConcurrentModificationException)
    封装特效记录--持续更新
    vue loading组件
    vue授权页面登陆之后返回之前的页面
    vue 路由权限
  • 原文地址:https://www.cnblogs.com/wangbiaohistory/p/14679004.html
Copyright © 2011-2022 走看看