熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

剖析ASP.NET AJAX的面向對象思想[3]

2013-11-13 11:03:08  來源: .NET編程 

  接口

  接口是類要實現的邏輯協議是對類進行集成的公共遵守的規范它能使多個類和同一個接口把實現定義和類的具體實現結合起來下面的例子定義了一個基類Tree和接口IFruitTreeApple和Banana這兩個類實現了接口IFruitTree但Pine類沒有實現接口IFruitTree

TyperegisterNamespace(DemoTrees);

DemoTreesIFruitTree = function() {}
DemoTreesIFruitTreePrototype = {
  bearFruit: function(){}
}
DemoTreesIFruitTreeregisterInterface(DemoTreesIFruitTree);


DemoTreesTree = function(name) {
  this_name = name;
}
DemoTreesTreeprototype = {
  returnName: function() {
    return this_name;
  }

  toStringCustom: function() {
    return thisreturnName();
  }

  makeLeaves: function() {}
}
DemoTreesTreeregisterClass(DemoTreesTree);


DemoTreesFruitTree = function(name description) {
  DemoTreesFruitTreeinitializeBase(this [name]);
  this_description = description;
}
DemoTreesFruitTreeprototypebearFruit = function() {
    return this_description;
}
DemoTreesFruitTreeregisterClass(DemoTreesFruitTree DemoTreesTree DemoTreesIFruitTree);

DemoTreesApple = function() {
  DemoTreesAppleinitializeBase(this [Apple red and crunchy]);
}
DemoTreesAppleprototype = {
  makeLeaves: function() {
    alert(Mediumsized and desiduous);
  }
  toStringCustom: function() {
    return FruitTree + DemoTreesApplecallBaseMethod(this toStringCustom);
  }
}
DemoTreesAppleregisterClass(DemoTreesApple DemoTreesFruitTree);

DemoTreesGrannySmith = function() {
  DemoTreesGrannySmithinitializeBase(this);
  // You must set the _description feild after initializeBase
  // or you will get the base value
  this_description = green and sour;
}
DemoTreesGrannySmithprototypetoStringCustom = function() {
  return DemoTreesGrannySmithcallBaseMethod(this toStringCustom) + its GrannySmith!;
}
DemoTreesGrannySmithregisterClass(DemoTreesGrannySmith DemoTreesApple);


DemoTreesBanana = function(description) {
  DemoTreesBananainitializeBase(this [Banana yellow and squishy]);
}
DemoTreesBananaprototypemakeLeaves = function() {
  alert(Big and green);
}
DemoTreesBananaregisterClass(DemoTreesBanana DemoTreesFruitTree);

 

DemoTreesPine = function() {
  DemoTreesPineinitializeBase(this [Pine]);
}
DemoTreesPineprototypemakeLeaves = function() {
  alert(Needles in clusters);
}
DemoTreesPineregisterClass(DemoTreesPine DemoTreesTree);

  Interfacejs腳本文件中定義了一個Tree基類和一個IFruitTree接口Apple和Banana兩個繼承類實現了IFruitTree接口而Pine類沒有實現IFruitTree接口運行Interfaceaspx點擊對象創建接口檢查調用接口方法體驗一下

枚舉

  枚舉是包含一組被命名的正整數常數的類你可以像訪問屬性一樣訪問它的值例如 myObjectcolor = myColorEnumred枚舉提供了一種很容易理解的整數表示下面的例子定義了一個以十六進制數表示的顏色被命名為有意義的名字的枚舉類型

TyperegisterNamespace(Demo);

// Define an enumeration type and register it
DemoColor = function(){};
DemoColorprototype =
{
  Red:   xFF
  Blue:   xFF
  Green: xFF
  White: xFFFFFF
}
DemoColorregisterEnum(DemoColor);

    運行Enumerationaspx選擇下拉框中的顏色腳本程序把背景色設為選中的枚舉類型DemoColor中的顏色

  反射

  反射用於檢查一個運行期程序的結構和組成是通過類Type的API來實現反射的這些方法使你能夠收集一個對象的信息例如它是繼承於哪個類它是否實現類某個特指的接口它是否是某個類的實例等

  下面的例子用反射的API測試GrannySmith類是否實現了前面的接口運行Reflectionaspx點擊檢查類型檢查繼承檢查接口體驗一下

  另外需要說明的一點是Microsoft AJAX Library是基於開放體系架構而開發的不僅僅用於aspnet還能用於其它體系架構中例如用在java中

[]  []  []  


From:http://tw.wingwit.com/Article/program/net/201311/15065.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.