superclass.constructor.call的用法

EXTJS中的类继承
superclass.constructor.call的用法
JavaScript中类的继承机制如下,有一个baseClass的类,然后为其定义两个方法,someMethod()和overwriteMethod()

var BaseClass = function(){
//do something
};
BaseClass.prototype.someMethod = function(){
//do something
};
BaseClass.prototype.overridenMethod = function(){
//do something
};

对于Extjs的类的继承,有几个函数需要注意一下

Ext.apply(obj, config, [defaults]) 将config对象的所有属性都复制到另一个对象obj上,第三个参数defaults可以用来提供默认值,不过通常指用前两个参数就够了。这个函数主要用在构造函数中,用来将配置复制到对象上。
Ext.applyIf(obj, config) 和Ext.apply的功能类似,唯一不同的是,这个函数只会将config对象中有,而obj对象中没有的属性复制到obj上。
Ext.extend(subclass, superclass, [overrides]) 用来继承已有的类

对于类的继承有几下几种方式:

1 javascript实现类的继承

var SubClass = function(){
BaseClass.call(this);
};
SubClass.prototype = new BaseClass();
SubClass.prototype .newMethod = function(){
//do something
};
SubClass.prototype.overridenMethod = function(){
//do something
};

2使用EXTjs的extend()函数

varSubClass = function() {
SubClass.superclass.constructor.call(this);
};
Ext.extend(SubClass, BaseClass, {
newMethod : function() {},
overriddenMethod : function() {}
};

3 extjs中替换constructor

// initComponent replaces the constructor: 
SubClass = Ext.extend(BaseClass, { 
    initComponent : function(){ 
        // call superclass initComponent 
      Ext.Container.superclass.initComponent.call(this); 

      this.addEvents({ 
         // add events 
      }); 
   } 
}
Ext.extend()函数提供了直接访问父类构造函数的途径,通过 SubClass.superclass.constructor.call(this);就可以直接调用父类的构造函数,这个函数的第一个参数总是 this,以确保父类的构造函数在子类的作用域里工作。
如果父类的构造函数需要传入参数,可以讲所需的参数直接传递给它: SubClass.superclass.constructor.call(this,config);这样就得到了一个继承了父类的所有属性和函数的子类。

下面是一个完整的类继承的例子

SuperClass=function(){
}
SuperClass.prototype.AA=function(){
 alert('aa');
}
SubClass=function(){
SubClass.superclass.constructor.call(this);          
}
Ext.extend(SubClass,SuperClass,{
BB:function(){alert('新方法');},
AA:function(){alert('重写方法');}
}//配置信息主要用来重写父类的方法和添加新方法
);
var sub=new SubClass();
sub.AA();
sub.BB();

结果是alert出重写方法,和新方法。

这里补充一个稍微复杂的例子,以下是两种不同的继承方式

IDB.WebsqlConsole = Ext.extend(IDB.MyTabPanel, {
initComponent:function() {
this.databasePanel = new Ext.Panel({title:'标题1'});
 this.tabPanel = new Ext.Panel({title:'标题2'})
 this.sqlConsole = new Ext.Panel({title:'标题3'});
Ext.apply(this, {
items:[
this.databasePanel , this.tabPanel , this.sqlConsole
 ],
getDatabasePanel:function() {
return this.databasePanel;
 }
 });
IDB.WebsqlConsole.superclass.initComponent.call(this);
}
});
IDB.ConsoleManager = function() {
IDB.ConsoleManager.superclass.constructor.call(this , {
 items:[new Ext.Panel({title:'标题1'})]
});
};
Ext.extend(IDB.ConsoleManager, IDB.MyTabPanel)

总结一下,对于EXTjs来说,有一个base类,子类使用extend来继承base类,extend有三个参数,第一个是子类即this,第二个参数是base类,第三个参数是config。另外子类在构造的时候使用subclase.superclass.construtor.call(this);来构造。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here is an example implementation of the ChangeToUpperCaseInputStream and ChangeToUpperCaseOutputStream classes: ```java import java.io.*; public class ChangeToUpperCaseInputStream extends FilterInputStream { public ChangeToUpperCaseInputStream(InputStream in) { super(in); } @Override public int read() throws IOException { int c = super.read(); return (c == -1 ? c : Character.toUpperCase((char) c)); } } public class ChangeToUpperCaseOutputStream extends FilterOutputStream { public ChangeToUpperCaseOutputStream(OutputStream out) { super(out); } @Override public void write(int b) throws IOException { super.write(Character.toUpperCase((char) b)); } } ``` In the above code, the ChangeToUpperCaseInputStream class extends FilterInputStream and overrides the read() method to read the data using the superclass's read() method and convert it to upper case before returning it. Similarly, the ChangeToUpperCaseOutputStream class extends FilterOutputStream and overrides the write() method to convert the data to upper case before calling the superclass's write() method to write it to the output stream. To use these classes in your main program, you can create an instance of ChangeToUpperCaseInputStream or ChangeToUpperCaseOutputStream and pass it to your FileInputStream or FileOutputStream, respectively. Here is an example: ```java import java.io.*; public class MyProgram { public static void main(String[] args) throws IOException { FileInputStream in = new FileInputStream("input.txt"); ChangeToUpperCaseInputStream upperCaseIn = new ChangeToUpperCaseInputStream(in); FileOutputStream out = new FileOutputStream("output.txt"); ChangeToUpperCaseOutputStream upperCaseOut = new ChangeToUpperCaseOutputStream(out); int c; while ((c = upperCaseIn.read()) != -1) { upperCaseOut.write(c); } upperCaseIn.close(); upperCaseOut.close(); } } ``` In the above code, we first create a FileInputStream and wrap it in a ChangeToUpperCaseInputStream. We also create a FileOutputStream and wrap it in a ChangeToUpperCaseOutputStream. We then read data from the input stream using the read() method of the ChangeToUpperCaseInputStream and write data to the output stream using the write() method of the ChangeToUpperCaseOutputStream. Finally, we close both streams to release any resources they are holding.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值