ES6 继承、多态简单demo
2017-10-16 11:02
465 查看
继承demo:
class Animal{
constructor(shoutVoice, speed){
this._shoutVoice = shoutVoice;
this._speed = speed;
}
get speed(){
return this._speed;
}
shout(){
console.log(this._shoutVoice);
}
run(){
console.log('本上仙的速度可是有' + this.speed);
}
}
class Dog extends Animal{
constructor(){
super('汪汪汪', '10m/s');
}
gnawBone(){
console.log('这是本狗最幸福的时候');
}
run(){
console.log('本狗的速度可是有' + this._speed);
super.run();
}
}
class PoodleDog extends Dog{
constructor(){
super();
this._breed = 'poodle';
}
get breed(){
return this._breed;
}
}
let poodleDog = new PoodleDog();
console.log(poodleDog.breed);
console.log(poodleDog.speed);
poodleDog.shout();
poodleDog.run()
poodleDog.gnawBone();
console.log:
poodle
10m/s
汪汪汪
本狗的速度可是有10m/s
本上仙的速度可是有10m/s
这是本狗最幸福的时候
多态demo:
class Animal{
eat(food){
console.log('"' + this.constructor.name + '"类没有eat()方法.');
}
}
class Snake extends Animal{}
class Dog extends Animal{
eat(food){
console.log('本狗在啃' + food);
}
}
class Cat extends Animal{
eat(food){
console.log('这只猫在吃' + food);
}
}
let snake = new Snake();
snake.eat('老鼠');
let dog = new Dog();
dog.eat('骨头');
let cat = new Cat();
cat.eat('鱼');
console.log:
"Snake"类没有eat()方法.
本狗在啃骨头
这只猫在吃鱼
class Animal{
constructor(shoutVoice, speed){
this._shoutVoice = shoutVoice;
this._speed = speed;
}
get speed(){
return this._speed;
}
shout(){
console.log(this._shoutVoice);
}
run(){
console.log('本上仙的速度可是有' + this.speed);
}
}
class Dog extends Animal{
constructor(){
super('汪汪汪', '10m/s');
}
gnawBone(){
console.log('这是本狗最幸福的时候');
}
run(){
console.log('本狗的速度可是有' + this._speed);
super.run();
}
}
class PoodleDog extends Dog{
constructor(){
super();
this._breed = 'poodle';
}
get breed(){
return this._breed;
}
}
let poodleDog = new PoodleDog();
console.log(poodleDog.breed);
console.log(poodleDog.speed);
poodleDog.shout();
poodleDog.run()
poodleDog.gnawBone();
console.log:
poodle
10m/s
汪汪汪
本狗的速度可是有10m/s
本上仙的速度可是有10m/s
这是本狗最幸福的时候
多态demo:
class Animal{
eat(food){
console.log('"' + this.constructor.name + '"类没有eat()方法.');
}
}
class Snake extends Animal{}
class Dog extends Animal{
eat(food){
console.log('本狗在啃' + food);
}
}
class Cat extends Animal{
eat(food){
console.log('这只猫在吃' + food);
}
}
let snake = new Snake();
snake.eat('老鼠');
let dog = new Dog();
dog.eat('骨头');
let cat = new Cat();
cat.eat('鱼');
console.log:
"Snake"类没有eat()方法.
本狗在啃骨头
这只猫在吃鱼
相关文章推荐
- C#多重继承 + 多态 + 扩展方法 简单示例
- C++多态、继承的简单分析
- objective c 中的继承和多态简单示意(二)
- 用一个简单示例来说明C#中的继承、封装和多态
- 4、面向对象以及winform的简单运用(继承与多态、命名空间与类库)
- 用一个简单示例来说明C#中的继承、封装和多态
- ECMA2015(ES6)简单入门-3-迭代器-生成器-定义类-__静态方法__继承
- RUBY的类封装,继承,多态简单演示
- 关于封装,继承,多态三个概念的简单理解
- Java打造简单的游戏数据库(继承与多态的应用)
- JS中简单的继承与多态
- Java回顾:用一个Demo来说明继承抽象类和实现接口的简单框架模型
- 简单理解C++的多态公有继承及虚函数
- objective c 中的继承和多态简单示意(二)
- ES6抽象、封装简单demo
- 关于java 继承和简单多态
- c++继承与多态概念的简单总结
- C# 学习之旅(二)简单的继承多态介绍
- JAVA的三大特征 封装继承多态- 简单总结
- java中面向对象三大思想(继承、封装、多态)简单应用