jscriptFrame(2) 使用Class基類定義接口

       接口是OO中重要的實現方法,可以實現對相似業務邏輯的抽象,是“解耦合”原則實現的重要途徑之一。 

      使用Class基類可以實現“接口”。

var Interface  = Class.extend({
        construct:$abstract;
        methordA:$abstract;
        methordB:$abstract;
}

Interface.implement 
= Interface.extend;
      ClassA “實現” Interface 接口:
var ClassA = Interface.implement({
        construct:function()
{
              
this.p1 = 1;
              
this.p2 = 2;
        }
,
        methordA:function()
{
             
return this.p1 - this.p2;
        }
,
        methordB:function()
{
             
return this.p1 + this.p2;
        }

}
)
      ClassB 也“實現” Interface 接口:
var ClassB = Interface.implement({
           construct:
function(){
                  
this.p1 = 1;
                  
this.p2 = 2;
           }
,
           methordA:
function(){
                 
return this.p1 * this.p2;
           }

           methordB:
function(){
                
return this.p1 / this.p2;
          }

}
)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章