JavaScript之面向对象中的多态

Created at 2016-07-27 Updated at 2017-01-06 Category Front-End Tag JavaScript

  • 多态:同一个父类继承出来的子类各有各的形态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
function Cat(){
this.eat = '肉';
}
function Tiger(){
this.color = '黑黄相间';
}
function Cheetah(){
this.color = '报文';
}
function Lion(){
this.color = '土黄色';
}
Tiger.prototype = Cheetah.prototype = Lion.prototype = new Cat();//共享一个祖先 Cat
var T = new Tiger();
var C = new Cheetah();
var L = new Lion();
console.log(T.color);
console.log(C.color);
console.log(L.color);
console.log(T.eat);
console.log(C.eat);
console.log(L.eat);

Table of Content

Site by Poetry using Hexo & Random
© 2015 - 2017

技术改变生活

Hide