zoukankan      html  css  js  c++  java
  • 面向对象编程思路

    <!DOCTYPE html>
    <html>
    
        <head>
            <meta charset="UTF-8">
            <title>面向对象编程思路</title>
        </head>
    
        <body>
        </body>
        <script type="text/javascript">
            // 把大象放进冰箱,需要几个步骤
    
            /*
            // ------------- 面向过程的编程思路 -------------
            // false : 门是关闭状态   true : 门是开启的状态
            var door = false;
            // false : 冰箱无物   true : 冰箱有物
            var box = false;
            
            // 第一步 : 打开冰箱的门
            function openDoor(){
                door = true;
            }
            openDoor();
            // 第二步 : 把大象放进去
            function putElephant(){
                if(door){
                    if(box){
                        box = false;
                    }
                    if(!box){
                        box = true;
                    }
                }
            }
            putElephant();
            // 第三步 : 把门关上
            function closeDoor(){
                if(door){
                    door = false;
                }
            }
            closeDoor();
            */
            // ---------------- 面向对象的编程思路 ------------------
    
            function Box(door, box) {
                this.door = door;
                this.box = box;
            }
            Box.prototype.openDoor = function() {
                this.door = true;
                return this;
            }
            Box.prototype.putElephant = function() {
                if(this.door) {
                    this.box = true;
                }
                return this;
            }
            Box.prototype.closeDoor = function() {
                if(this.door) {
                    this.door = false;
                }
                return this;
            }
            // 调用
            var box = new Box(false, false);
             box.openDoor();
             box.putElephant();
            box.closeDoor();
        </script>
    
    </html>
  • 相关阅读:
    Linux strip
    有趣的BUG
    GDB watch std::string size
    Redis Cluster Lua
    Double Buffer
    Yarn架构
    天池公交客流预测比赛
    hashmap,ConcurrentHashMap与hashtable的区别
    fail-fast和fail-safe
    常见机器学习算法优缺点
  • 原文地址:https://www.cnblogs.com/zbly/p/10175619.html
Copyright © 2011-2022 走看看