使用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();