让js支持oo的所有特性,不知道可不可以,再说把。
继承,重定义,隐藏
<script>
//类A,做为基类
function ClassA(name){
public:
this.name = name;
this.sayName = function(){alert('基类A的sayName函数');}
private:
var pri_name = 'ClassA_pri';
}
function ClassB(name){
Extends:
ClassA.call(this,name);
public:
this.Super = new ClassA(name);//super为关键字,无法使用
this.sayName = function(){alert('继承于A的类B的sayName函数');}
this.sayName2 = function(args){alert('参数:' + args);}
this.callPri_Func = function(){alert('共有方法callPri_Func调用:' + pri_Func())}
private:
var pri_name = 'ClassB_pri';
function pri_Func(){ return '私有变量pri_name:' + pri_name;}
}
//测试================== ==================
==========
var objB = new ClassB('ClassB');
objB.sayName();
//调用基类方法
objB.Super.sayName();
//objB.pri_Func();无法调用私有方法
objB.callPri_Func();
objB.sayName2('准备多态....');
</script>
继承,重定义,隐藏
<script>
//类A,做为基类
function ClassA(name){
public:
this.name = name;
this.sayName = function(){alert('基类A的sayName函数');}
private:
var pri_name = 'ClassA_pri';
}
function ClassB(name){
Extends:
ClassA.call(this,name);
public:
this.Super = new ClassA(name);//super为关键字,无法使用
this.sayName = function(){alert('继承于A的类B的sayName函数');}
this.sayName2 = function(args){alert('参数:' + args);}
this.callPri_Func = function(){alert('共有方法callPri_Func调用:' + pri_Func())}
private:
var pri_name = 'ClassB_pri';
function pri_Func(){ return '私有变量pri_name:' + pri_name;}
}
//测试==================
var objB = new ClassB('ClassB');
objB.sayName();
//调用基类方法
objB.Super.sayName();
//objB.pri_Func();无法调用私有方法
objB.callPri_Func();
objB.sayName2('准备多态....');
</script>
![](https://i-blog.csdnimg.cn/blog_migrate/d86522e41a1c53b89e1ee8cb29d370e3.gif)