zoukankan      html  css  js  c++  java
  • JS使用面向对象思想的示例_封装继承多态_class_extend_ES6语法使用

    使用JS输出以下语句:

    1只 灰色 的 灰鸭 发着 嘎嘎声 用翅膀飞行
    1只 红色 的 红头鸭 沉默着 用翅膀飞行
    1只 黄色 的 橡皮鸭 发着 嘎嘎声 用意念飞行
    1只 白色 的 棉花鸭 是不能飞的哑巴

    JS示例--主要学习用法和面向对象方式的写法-这样更有趣:

    class Duck2{
        constructor(num,color,name,flyTool,voice){
            this.num=num;
            this.color=color; 
            this.name=name;  
            this.flyTool=flyTool;
            this.voice=voice;
            this.quack=function(){
                if(this.voice && this.voice!=""){
                    return "发着 "+this.voice;
                }else{
                    return "沉默着";
                }
            };
            this.fly=function(){
                if(this.flyTool && this.flyTool!=""){
                    return "用"+this.flyTool+"飞行";
                }else{
                    return "不能飞行";
                }
            };
            this.print=function(){
                console.info(this.num+"只 "+this.color+" 的 "+this.name+" "+this.quack()+" "+this.fly());
            }
        }
    }
    
    class DisableDuck extends Duck2{
        constructor(num,color,name){
            super(num,color,name,"","");
    
            this.print=function(){
                console.info(this.num+"只 "+this.color+" 的 "+this.name+" 是不能飞的哑巴");
            }
        }
    }
    
    var normalDuck=new Duck2(1,"灰色","灰鸭","翅膀","嘎嘎声");
    normalDuck.print();
    normalDuck=new Duck2(1,"红色","红头鸭","翅膀","");
    normalDuck.print();
    normalDuck=new Duck2(1,"黄色","橡皮鸭","意念","嘎嘎声");
    normalDuck.print();
    normalDuck=new DisableDuck(1,"白色","棉花鸭");
    normalDuck.print();
    *感谢您的阅读。喜欢的、有用的就请大哥大嫂们高抬贵手“推荐一下”吧!你的精神 支持是博主强大的写作动力。欢迎转载!
    *博主的文章是自己平时开发总结的经验,由于博主的水平不高,不足和错误之处在所难免,希望大家能够批评指出。
    *我的博客: http://www.cnblogs.com/lxhbky/
  • 相关阅读:
    如何优雅的,灵活的把想要展示的数据传给前端
    利用AES算法加密数据
    Swing-布局管理器应用--WIN7上计算器的UI实现
    Swing-GridBagLayout用法-入门
    Swing-setMaximumSize()用法-入门
    Swing-setOpaque()用法-入门
    Swing-setBounds()用法-入门
    Swing-setAlignmentX()用法-入门
    Swing-setBorder()用法-入门
    Swing-布局管理器之GridLayout(网格布局)-入门
  • 原文地址:https://www.cnblogs.com/lxhbky/p/14938762.html
Copyright © 2011-2022 走看看