Share via


class 陳述式

更新:2007 年 11 月

宣告類別的名稱以及組成類別的變數、屬性 (Property) 和方法的定義。

 [modifiers] class classname [extends baseclass] [implements interfaces]{    [classmembers] }

引數

  • 修飾詞
    選擇項。修飾詞,控制類別的可視性和行為。

  • classname
    必要項。class 的名稱,依照標準變數命名規範來命名。

  • extends
    選擇項。關鍵字,表示 classname 類別延伸 baseclass。如果沒有使用這個關鍵字,會建立一個延伸 System.Object 的標準 JScript 基底類別。

  • baseclass
    選擇項。被延伸的類別名稱。

  • implements
    選擇項。關鍵字,表示 classname 類別實作一或多個介面。

  • interfaces
    選擇項。介面名稱的逗號分隔清單。

  • classmembers
    選擇項。classmembers 可以是方法或建構函式宣告 (以 function 陳述式定義)、屬性宣告 (以 function getfunction set 陳述式定義)、欄位宣告 (以 varconst 陳述式定義)、初始設定式宣告 (以 static 陳述式定義)、列舉型別宣告 (以 enum 陳述式定義) 或巢狀類別宣告。

備註

類別可以用來建立執行個體 (Instance) 或做為其他類別的基底,視類別的修飾詞而定。如果類別是以 abstract 修飾詞標記,則可以做為其他類別要擴充的基底類別,但是無法建立 abstract 類別的執行個體。如果類別是以 final 修飾詞標記,則類別的執行個體可以使用 new 運算子來建立,但是類別不能成為基底。

在類別中,方法和建構函式可以多載。因此,多重方法 (或建構函式) 可能會有相同名稱。多載類別成員可以由它們唯一簽章來分辨,簽章是由成員的名稱和它的每個型式參數的資料型別組成。多載允許群組方法的類別具有類似的功能。

類別可以使用 extends 關鍵字來繼承現有基底類別的功能。可以宣告新方法 (此新方法具有與新類別方法相同的簽章) 來覆寫基底類別的方法。新類別中的方法可以使用 super 陳述式來存取基底類別的覆寫成員。

類別可以使用 implements 關鍵字在一或多個介面上模式化。類別無法由介面繼承任何行為,因為介面沒有提供任何成員的實作。介面提供具「簽章」的類別,在與其他類別互動時可以使用。除非實作介面的類別是 abstract,否則介面中定義的每個方法都必須提供實作。

可以使用修飾詞讓類別執行個體的行為更像 JScript 物件。若要讓類別執行個體能夠處理動態加入的屬性 (Property),請使用 expando 修飾詞,這會為類別自動建立預設的索引屬性。只有 expando 屬性 (Property) 會使用 JScript Object 物件的方括弧標記法來存取。

範例 1

下列範例建立具各種欄位和方法的 CPerson 類別,其欄位和方法的細節已經省略。CPerson 類別做為第二個範例中 CCustomer 類別的基底類別。

// All members of CPerson are public by default.
class CPerson{
   var name : String;
   var address : String;

   // CPerson constuctor
   function CPerson(name : String){
      this.name = name;
   };

   // printMailingLabel is an instance method, as it uses the
   // name and address information of the instance.
   function printMailingLabel(){
      print(name);
      print(address);
   };

   // printBlankLabel is static as it does not require
   // any person-specific information.
   static function printBlankLabel(){
      print("-blank-");
   };
}

// Print a blank mailing label.
// Note that no CPerson object exists at this time.
CPerson.printBlankLabel();

// Create a CPerson object and add some data.
var John : CPerson = new CPerson("John Doe");
John.address = "15 Broad Street, Atlanta, GA 30315";
// Print a mailing label with John's name and address.
John.printMailingLabel();

本程式碼的輸出為:

-blank-
John Doe
15 Broad Street, Atlanta, GA 30315

範例 2

CCustomer 類別衍生自 CPerson,具有不適用於 CPerson 類別的泛型成員的其他欄位和方法。

// Create an extension to CPerson.
class CCustomer extends CPerson{
   var billingAddress : String;
   var lastOrder : String;

   // Constructor for this class.
   function CCustomer(name : String, creditLimit : double){
      super(name); // Call superclass constructor.
      this.creditLimit = creditLimit;
   };

   // Customer's credit limit. This is a private field
   // so that only member functions can change it. 
   private var creditLimit : double;
   // A public property is needed to read the credit limit.
   function get CreditLimit() : double{
      return creditLimit;
   }
}

// Create a new CCustomer.
var Jane : CCustomer = new CCustomer("Jane Doe",500.);
// Do something with it.
Jane.billingAddress = Jane.address = "12 Oak Street, Buffalo, NY 14201";
Jane.lastOrder = "Windows 2000 Server";
// Print the credit limit.
print(Jane.name + "'s credit limit is " + Jane.CreditLimit);
// Call a method defined in the base class.
Jane.printMailingLabel();

這部分的程式碼輸出是:

Jane Doe's credit limit is 500
Jane Doe
12 Oak Street, Buffalo, NY 14201

需求

.NET 版本

請參閱

參考

interface 陳述式

function 陳述式

function get 陳述式

function set 陳述式

var 陳述式

const 陳述式

static 陳述式

new 運算子

this 陳述式

super 陳述式

其他資源

修飾詞