導航:首頁 > 方法技巧 > extjs如何調用父類的方法

extjs如何調用父類的方法

發布時間:2022-05-11 10:23:10

『壹』 在子類構造器中調用父類方法

使用super關鍵字在子類的構造器里調用父類的普通方法,例如:

/**
*父類
*
*@authorqd
*
*/
classF{

publicvoidteatA(){
System.out.println("teatA方法");
}
}

/**
*子類
*
*@authorqd
*
*/
classSextendsF{

//子類構造器中調用父類普通方法
publicS(){
super.teatA();
}
}

/**
*測試類
*
*@authorqd
*
*/
publicclassTest1{

publicstaticvoidmain(String[]args){

Ss=newS();
}
}

運行結果:

『貳』 extjs 初始化函數 中的 this.callParent(arguments);類似於繼承么就是繼承父類的對應的初始化函數

主要是為了覆蓋父類的同名方法...但是又需要父類方法同樣的動作的時候...

//比如一個grid父類
Ext.define("baseGrid",{
extend:"Ext.grid.Panel",
initComponent:function(){
//這里設置表格的工具欄,用一個createDockedItems方法生成
this.dockedItems=this.createDockedItems();
this.callParent();
},
//這里是生成表格的工具欄,默認的只有一個分頁欄
createDockedItems:function(){
return[{
xtype:"paging",dock:"bottom",store:this.store
}]
}
});

//然後grid1繼承這個類
Ext.define("grid1",{
extend:"baseGrid",
//這里grid1不僅有分頁欄,還需要有添加,修改,刪除的工具欄
createDockedItems:function(){
vartbs=this.callParent();//這里調用callParent,就是父類的方法,返回父類方法中的內容,就是那個分頁欄
//這里添加一個新的工具欄,包括添加修改按鈕
tbs.push({xtype:"toolbar",items:[
{text:"添加"},
{text:"修改"}
]});
returntbs;
}
});

//其實主要作用是為了覆蓋,而同時又不是全部都覆蓋,所以有了這個方法

『叄』 子類怎麼調用父類的方法和構造函數

調用父類的方法,直接寫就可以了;
調用父類的構造方法,在子類的構造方法裡面調用super();只能寫在構造方法的第一句

『肆』 子類繼承父類,怎麼調用父類的方法

不是這個意思。所謂「實例化子類的對象前要先實例化父類中的內容,為父類初始化」,是指當子類正在實例化時,總是鏈式調用父類構造方法初始化父類空間。換句話說,子類的構造方法必須先調用父類的構造方法,完了才能幹別的初始化工作

『伍』 extjs調用iframe父級函數

1、以下解決方案基於extjs4.2.1 架構採用extjs MVC。
頁面結構為左側菜單樹,右側TabPanel,點擊菜單樹,動態在tabpanel內新建利用樹的id生成的tabpanel頁,當然了,iframe的內容就是樹的節點herf,文中加粗部分就是這個過程的實現。
2、常式:

Ext.define('Sys.controller.MenuController',{
extend:'Ext.app.Controller',
refs:[{
ref:'menuTreeView',
selector:'menutreeview'
},
{
ref:'contentCardView',
selector:'contentcardview'
}
],
stores:['SysTree'],

init:function(){
this.control({
'menutreeview':{
beforeitemexpand:this.onItemExpand,
beforeitemclick:this.onBeforeItemClick,
itemclick:this.onClickTree
}
})
},

onItemExpand:function(node,optd){
varstore=this.getSysTreeStore();
varmProxy=store.getProxy();
mProxy.setExtraParam("id",node.get("id"));

},
onBeforeItemClick:function(viewtree,record,item,index,e,eOpts){

e.stopEvent();
},

『陸』 在js中,子類如何調用父類中方法如下圖示:

//子類Student
function Student(name,age,sex,phone){
//繼承方法

Person.call(this,name,age);
//添加自己的屬性
this.sex=sex;
this.phone=phone;
//添加自己的方法
this.say();

}
//繼承父類的屬性
for(var i in Person.prototype){
Student.prototype[i]=Person.prototype[i];

}
//重寫父類方法
Student.prototype.say()
{
alert(this.phone+' 'this.sex);

}

『柒』 如何在java中子類中父類的對象如何調用父類的方法

對於有繼承關系的類,子類可以通過這個關鍵字調用父類中的方法。
比如:super.query();
此句話的意思是調用父類中的非私有方法query。

一般的用super關鍵字,調用類中的父類中重載構造方法。
比如:父類有個構造方法public A(){},同時又寫了一個重載的構造方法public A(String name);那麼,在子類中可以使用super(name)指明調用父類的哪個構造方法進行實例化父類對象。

大概就是這樣的!

『捌』 請教關於extjs繼承jcrop的問題

Ext中實現類的繼承
Js代碼
extend (Object subclass,Object superclass,[Object overrides] : Object)

第一個參數:子類
第二個參數:父類
第三個參數:要覆蓋的屬性。
這里需要強調一下,子類繼承下來的是父類中通過superclass.prototype方式定義的屬性(包括用此方法定義的函數),而不繼承superclass中的定義的屬性和方法,如果子類中的方法名與父類中相同則會覆蓋。例子
父類
Js代碼
BaseClass = function() {
f1 = function() {
alert("f1 in base");
},
f2 = function() {
alert("f2 in base");
}
}
BaseClass.prototype = {
f3 : function() {
alert("f3 in base");
},
f4 : function() {
alert("f4 in base");
}
};

子類
Js代碼
ChildClass = function() {
}
// 繼承
Ext.extend(ChildClass, BaseClass, {
f1 : function() {
alert("f1 in child");
},
f3 : function() {
alert("f3 in child");
}
});

實例化
Js代碼
var b = new ChildClass();
b.f1();// 調用子類中的實現
//b.f2();// 會報錯,因為子類中沒有該方法,並且父類中定義的f2是內部變數,作用域只有在內部可見(閉包)
b.f3();// 繼承並覆蓋,調用子類的中的實現
b.f4();// 繼承,調用父類的中的實現

補充:通過對 JavaScript 的原型繼承的了解,可以知道,實例變數的優先順序是高於 prototype 的,參見我之前寫的文章javascript中靜態方法、實例方法、內部方法和原型的一點見解 又如以下例子:
父類
Js代碼
BaseClass = function() {
this.f1 = function() {
alert("f1 in base");
},
this.f2 = function() {
alert("f2 in base");
}
}

子類
Js代碼
ChildClass = function() {
ChildClass.superclass.constructor.call( this );
}
// 繼承
Ext.extend(ChildClass, BaseClass, {
f1 : function() {
alert("f1 in child");
},
f3 : function() {
alert("f3 in child");
}
});

實例化
Js代碼
var b = new ChildClass();
b.f1();// 調用父類中的實現,注意不會調用子類中的實現
b.f2();// 調用父類中的實現
b.f3();// 調用子類中的實現

分析:在 ChildClass.superclass.constructor.call(this); 這句上, BaseClass 的 f1 成了 ChildClass 的變數,而不是 ChildClass.prototype 。由於實例變數的優先順序是高於 prototype 的,所以上面的這個代碼是達不到 override 的功能的。

了解了以上知識,下面講解一下extend的實現,先看最簡單的繼承,實現原理,1、將子類的原型prototype設置為父類的一個實例,也就是說把父類的實例賦值給子類的prototype(這樣子類就有了父類原型的所有成員),2、重新將子類原型的構造器設置為子類自己,也就是把子類賦值給子類原型的構造器。
以下代碼把 subFn 的 prototype 設置為 superFn 的一個實例,然後設置 subFn.prototype.constructor 為 subFn。
Js代碼
function Extend(subFn, superFn) {
subFn.prototype = new superFn();
subFn.prototype.constructor = subFn;
}

//父類
function Animal() {
this.say1 = function() {
alert("Animal");
}
}
//子類
function Tiger() {
this.say2 = function() {
alert("Tiger");
}

}
//繼承應用
Extend(Tiger, Animal);
var tiger = new Tiger();
tiger.say1();// "Animal"
tiger.say2();// "Tiger"

可以看到最簡單的繼承只做了兩件事情,一是把 subFn 的 prototype 設置為 superFn 的一個實例,然後設置 subFn . prototype . constructor 為 subFn 。

Ext.extend 的代碼

Ext.extend 函數中用到了 Ext.override ,這個函數把第二個參數中的所有對象復制到第一個對象的 prototype 中。首先貼上 Ext.override 函數的代碼:
Js代碼
/**
* Adds a list of functions to the prototype of an existing class, overwriting any existing methods with the same name.
* Usage:<pre><code>
Ext.override(MyClass, {
newMethod1: function(){
// etc.
},
newMethod2: function(foo){
// etc.
}
});
</code></pre>
* @param {Object} origclass The class to override
* @param {Object} overrides The list of functions to add to origClass. This should be specified as an object literal
* containing one or more methods.
* @method override
*/
override : function(origclass, overrides){
if(overrides){
var p = origclass.prototype;
Ext.apply(p, overrides);
if(Ext.isIE && overrides.hasOwnProperty('toString')){
p.toString = overrides.toString;
}
}
}

以下是 Ext.extend的代碼
Js代碼
/**
* 繼承,並由傳遞的值決定是否覆蓋原對象的屬性
* 返回的對象中也增加了 override() 函數,用於覆蓋實例的成員
* @param { Object } subclass 子類,用於繼承(該類繼承了父類所有屬性,並最終返回該對象)
* @param { Object } superclass 父類,被繼承
* @param { Object } overrides (該參數可選) 一個對象,將它本身攜帶的屬性對子類進行覆蓋
* @method extend
*/
extend : function(){
// inline overrides
var io = function(o){
for(var m in o){
this[m] = o[m];
}
};
var oc = Object.prototype.constructor;

//匿名函數實現
//三個參數sb、sp、overrides分別代表subClass(子類)、superClass(父類)及覆蓋子類的配置參數
return function(sb, sp, overrides){
if(typeof sp == 'object'){//傳遞兩個參數時superClass, overrides
overrides = sp;
sp = sb;
sb = overrides.constructor != oc ? overrides.constructor : function(){sp.apply(this, arguments);};
}
var F = function(){},//定義一空函數,用來賦給其對象時清空該對象
sbp,
spp = sp.prototype;

F.prototype = spp;
// 注意下面兩句就是JavaScript中最簡單的繼承實現。
sbp = sb.prototype = new F();//清空
sbp.constructor=sb;
// 添加了 superclass 屬性指向 superclass 的 prototype
sb.superclass=spp;
if(spp.constructor == oc){
spp.constructor=sp;
}
// 為 subClass 和 subClassPrototype 添加 override 函數
sb.override = function(o){
Ext.override(sb, o);
};
sbp.superclass = sbp.supr = (function(){
return spp;
});
sbp.override = io;
// 覆蓋掉子類 prototype 中的屬性
Ext.override(sb, overrides);
//為子類加上類方法:extend
sb.extend = function(o){return Ext.extend(sb, o);};
return sb;
};
}(),

代碼中進行了太多的簡寫,看起來不是特別方便,把代碼中的簡寫補全,代碼如下:
Js代碼
extend : function(){
// inline overrides
var inlineOverride = function(o){
for(var m in o){
this[m] = o[m];
}
};
var oc = Object.prototype.constructor;

return function(subFn, superFn, overrides){
if(typeof superFn == 'object'){
// 如果 superFn 也是對象的話(一般來說 superFn 這里放的是父類的構造函數),那麼第三個參數 overrides 參數相當於被忽略掉
overrides = superFn;
superFn = subFn;
//重新定義了函數 subFn
subFn = overrides.constructor != oc ? overrides.constructor : function(){superFn.apply(this, arguments);};
}
var F = function(){},
subFnPrototype,
superFnPrototype = superFn.prototype;

F.prototype = superFnPrototype;
subFnPrototype = subFn.prototype = new F();
subFnPrototype.constructor=subFn;
subFn.superclass=superFnPrototype;
if(superFnPrototype.constructor == oc){
superFnPrototype.constructor=superFn;
}
subFn.override = function(o){
Ext.override(subFn, o);
};
subFnPrototype.superclass = subFnPrototype.supr = (function(){
return superFnPrototype;
});
subFnPrototype.override = inlineOverride;
Ext.override(subFn, overrides);
subFn.extend = function(o){return Ext.extend(subFn, o);};
return subFn;
};
}()

代碼中糅合了傳兩個參數和三個參數的實現,理解起來不容易明白,我們可以把代碼拆分為兩個參數和三個參數的實現,如下兩個參數的 Ext.extend 代碼

Js代碼
function extend() {
// inline overrides
var inlineOverride = function(o) {
for (var m in o) {
this[m] = o[m];
}
};
return function(superFn, overrides) {
// 定義返回的子類
var subFn=overrides.constructor != Object.prototype.constructor ? overrides.constructor : function(){superFn.apply(this, arguments);};
//以下為中間變數,或叫過程變數
var F = function() {

}, subFnPrototype, superFnPrototype = superFn.prototype;

F.prototype = superFnPrototype;//F中含有了所有 superFn.prototype 中的功能

// 注意下面兩句就是JavaScript中最簡單的繼承實現。
subFnPrototype = subFn.prototype = new F();
subFnPrototype.constructor = subFn;
//改變父類實例對象中的constructor,使其指向自身的構建函數
if(superFnPrototype.constructor == oc){
superFnPrototype.constructor=superFn;
}
// 添加了 superclass 屬性指向 superFn 的 prototype
subFn.superclass = superFnPrototype;

// 為 subFn 和 subFnPrototype 添加 override 函數
subFn.override = function(obj) {
Ext.override(subFn, obj);
};
subFnPrototype.override = inlineOverride;

// 覆蓋掉子類 prototype 中的屬性
Ext.override(subFn, overrides);
//為子類加上類方法:extend
subFn.extend=function(o){
Ext.extend(subFn,o);
}
return subFn;
};

};

從注釋中可以看到,做的工作很簡單,只是定義一個 subFn 函數,這個函數中會調用 superFn 函數。定義了 subFn 以後,就使用上面的最簡單的繼承方式實現繼承。然後為 subFn 和 subFn 的 prototype 添加了一個 override 函數。最後的 Ext.override(subFn, overrides); 把 overrides 中的函數寫入 subFn 的 prototype 中。
以下是傳兩個參數的簡單例子

Js代碼
var BaseClass = function(){};
BaseClass.prototype = {
method1 : function(){
alert('father class');
}
};
//兩個參數的繼承
var subClass = Ext.extend(BaseClass,{
method2 : function(){
alert('sub class');
}
});

var sub = new subClass();
sub.method1();
sub.method2();

『玖』 js中子類重寫父類中方法, 重寫方法中用到父類的方法怎麼調用呢

子類重寫方法是在基類有此方法重寫,那麼聲名一個子類的對象,調用的方法是子類的方法,
通過base轉而調用父類中的方法,最終目的還是調用父類中的方法。
還有重寫方法可以改變基類方法的作用,可以實現其他的效果,重寫方法,和基類同名方法是兩個不同實現的方法,
最主要你要分清楚 重寫跟基類中的方法不一定實現同樣的效果,還有基類也不一定知道派生類中是否重寫了這個方法

閱讀全文

與extjs如何調用父類的方法相關的資料

熱點內容
解鎖加密在哪裡設置方法 瀏覽:80
座便池安裝方法 瀏覽:265
國醫大師治療腦中風的方法 瀏覽:611
小米手機瀏覽器在哪裡設置方法 瀏覽:108
濕疹鑒別的方法 瀏覽:358
乳房刮痧方法視頻 瀏覽:143
資本成本權重的計算方法 瀏覽:813
薄餅檔的使用方法 瀏覽:239
有電阻電壓計算方法 瀏覽:952
安裝櫃子滑道的方法 瀏覽:675
優思明的正確服用方法 瀏覽:772
適用於定性研究的定量方法 瀏覽:706
球隊阻力帶訓練方法籃球 瀏覽:687
上火惡心有沒有快速治療方法 瀏覽:826
usb公頭焊接方法視頻 瀏覽:558
鹽水洗臉正確方法 瀏覽:462
pvc管熱熔器使用方法 瀏覽:15
靜脈曲張最好的治療方法 瀏覽:897
電腦新養方法 瀏覽:787
福來恩滴劑使用方法圖 瀏覽:627