這篇是高級用法,主要是講:如果你在實現類(實現基類的類)中聲明和基類相同的屬性或者方法的話,
實現類的方法或者函數,會被基類取代掉的。很有意思,動手實驗下哦
- var BaseClass = new Class({
- initialize : function(input_value){
- this.inputValue = input_value;
- },
- testFunction : function(){
- alert(" this is a base class function");
- },
- definedValue : " Defined in BaseClasss"
- });
- var ImpleClass = new Class({
- Implements : BaseClass,
- //下面的這些功能都在BaseClass中定義了
- definedValue : 'Defined in ImpleClass',
- testFuncton : function(){
- alert('this function is defined in ImpleClass');
- },
- //下面這些都是自定義的
- anotherDefinedValue : ' another value in ImpleClass',
- anotherTestFunction : function(){
- alert('another function in ImpleClass');
- }
- });
請注意,在實現類中重新定義了testFunction和definedVariable,
就像我們添加新的函數和變量一樣。特別需要注意的是:如果你想在實現類中定義
一個已經在基類中定義了的函數或者變量,基類中的定義將會取代實現類中的定義。
如果不明白,看看下面的例子就知道了。
- var impleClass = new ImpleClass();
- impleClass.testFunction();
- alert("impleClass.testValue:"+impleClass.definedValue);
- //顯示自定義的
- impleClass.anotherTestFunction();
- alert("impleClass.testValue:"+impleClass.anotherDefinedValue);