Welcome 微信登录

首页 / 脚本样式 / JavaScript / Javascript玩转继承(二)

Javascript玩转继承(二)2010-04-09 博客园 飞林沙在《》中,我主要介绍了对象伪装来实现继承。我们在这里先来说一下这种方式的优缺点。

毋庸置疑,这种方式是比较容易理解的,在子类中调用父类的构造函数。另外,这种方法最大的一个优点就是说构造继承可以实现多继承,复习下这个代码:

function A()
{ }
function B()
{ }
function C()
{
this.father=A;
this.father();
delete this.father;
this.father=B;
this.father();
delete this.father;
}

但是这种方式也有着这样和那样的缺点:

熟悉面向对象的我们来看这样一段C#代码:

class Program
{
static void Main(string[] args)
{
B b=new B();
bool temp = (typeof(A)).IsInstanceOfType(b);
Console.WriteLine(temp);
}
}
class A
{
public A()
{

}
}
class B : A
{
public B()
{

}
}

结果呢?b当然是A的一个实例: