Microsoft AJAX Library中的物件導向(二)

來源:互聯網
上載者:User

接上篇http://www.cnblogs.com/Brave-Heart/archive/2008/06/25/1229839.html

介面
    ·建構函式拋出異常
    ·所有的方法拋出異常
    ·registerInterface方法用來註冊介面
    ·介面不能繼承其他的介面
    ·定義(和定義類很相似)
       MyNamespace.IMyInterface = function()
      {
          throw Error.notImplemented();  
      }
      MyNamespace.IMyInterface.prototype=
     {
         myMethod:function()
        {
           throw Error.notImplemented();
        },
        ......   
     }
     MyNamespace.IMyInterface.registerInterface("MyNamespace.IMyInterface");

    ·實現介面
       使用registerClass方法傳入額外的參數實現介面
       MyNamespace.MyClass.registerClass("MyNamespace.MyClass",null,MyNamespace.IMyInterface1,IMyInterface2,...); 
       //第二個參數null表示沒有父類

      【code demo】:

       <asp:ScriptManager ID="ScriptManager1" runat="server">
       </asp:ScriptManager>
     
     
       <script language="javascript" type="text/javascript">
     
       Type.registerNamespace("dongdeNamespace");
     
     
     
       dongdeNamespace.Worker = function(name)
        {
           this._name = name ? name:"(unknown)";
           this._bornyear = 0;
        }
       
        dongdeNamespace.Worker.prototype=
        {
           get_name:function()
           {
              return this._name;
           },
          
          
           get_bornyear:function()
           {
             
              return this._bornyear;
           },
           set_bornyear:function(value)
           {
              this._bornyear=value;
          
           },
          
          
           getAge:function()
           {
              return 2008-this._bornyear;
          
           },
          
          
           getWorkerDescript:function()
           {
             
              return String.format("{0}年齡:{1},出生於:{2}",this._name,this.getAge(),this.get_bornyear());
           },   
       
           //抽象方法
           getSalary:function()
           {
              throw Error.notImplemented();
          
           }
        }
       
       
      //////////////////////////////////////////////////
      dongdeNamespace.IWorker = function()
      {
         throw Error.notImplemented();
      }
      dongdeNamespace.IWorker.prototype=function()
      {
         getSex:function()
         {
            throw Error.notImplemented();
         }
        
     
      }
     
      dongdeNamespace.IWorker.registerInterface("dongdeNamespace.IWorker");
      dongdeNamespace.Worker.registerClass("dongdeNamespace.Worker",null,dongdeNamespace.IWorker);
      //dongdeNamespace.Worker.registerClass("dongdeNamespace.Worker");
     
      var newWorker = new dongdeNamespace.Worker();
     
      alert(  dongdeNamespace.IWorker.isImplementedBy(newWorker) ); //isImplementedBy反射方法,
                判斷newWorker對象有沒有實現dongdeNamespace.IWorker介面
      
      </script>

枚舉
      ·枚舉即為number
      ·很大的好處就是爭強可讀性,發現很多初寫程式的人表示某種狀態都是用數字1,2,3...來表示,這樣做代碼的可讀性不強,不好理解
      ·每個枚舉類型都有toString和parse方法
      ·定義
        MyNamespace.MyEnum = function()
        {
          throw Error.notImplemented(); 
        }
        MyNamespace.MyEnum.prototype=
        {
            Item1: 1,
            Item2: 2,
            Item3: 3 
        }
        MyNamespace.MyEnum.registerEnum("MyNamespace.MyEnum",[,true]);

  

       【code demo】:稍微簡單的列子
         
       <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
       
        <script language="javascript" type="text/javascript">
       
         Type.registerNamespace("MyNamespace");
        
         MyNamespace.MyEnum = function()
        {
           throw Error.notImplemented();
 
        }
        MyNamespace.MyEnum.prototype=
        {
          Item1: "dongde",
          Item2: "ling",
          Item3: "mary" 
        }
        MyNamespace.MyEnum.registerEnum("MyNamespace.MyEnum");
       
       
        alert(MyNamespace.MyEnum.Item1);
        alert(MyNamespace.MyEnum.toString(MyNamespace.MyEnum.Item1));            
       
        </script>

反射方法

       Type.prototype.getBaseType 
       Type.prototype.getInterfaces
       Type.prototype.getName
       Type.prototype.implementsInterface
       Type.prototype.inheritsFrom
       Type.prototype.isImplementedBy
       Type.prototype.isInstanceOfType
       Type.getRootNamespaces
       Type.isClass
       Type.isInterface
       Type.isNamespace
       Type.isEnum
       Type.isFlags
       Type.parse
       Object.getType
       Object.getTypeName

   

 

 

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.