Share via


static 修飾詞

宣告類別成員屬於類別而不是類別的執行個體。

static statement

引數

  • statement
    必要項。 類別成員定義。

備註

static 修飾詞強調成員是屬於類別本身而不是類別的執行個體。 特定應用程式中只存在一份靜態成員的複本,即使已經建立許多類別的執行個體。 您只能用類別的參考而不是執行個體的參考來存取靜態成員。 然而,在類別成員宣告中,靜態成員可以用 this 物件進行存取。

類別的成員可以使用 static 修飾詞來標記。 類別、介面和介面的成員不能使用 static 修飾詞。

您不能將 static 修飾詞與任何繼承修飾詞 (abstractfinal) 或版本安全修飾詞 (hide 和 override) 結合使用。

不要混淆 static 修飾詞和 static 陳述式。 static 修飾詞表示成員是屬於類別本身而不是類別的執行個體。

範例

以下範例說明 static 修飾詞的用法。

class CTest {
   var nonstaticX : int;      // A non-static field belonging to a class instance.
   static var staticX : int;  // A static field belonging to the class.
}

// Initialize staticX. An instance of test is not needed.
CTest.staticX = 42;

// Create an instance of test class.
var a : CTest = new CTest;
a.nonstaticX = 5;
// The static field is not directly accessible from the class instance.

print(a.nonstaticX);
print(CTest.staticX);

本程式的輸出為:

5
42

需求

.NET 版本

請參閱

參考

expando 修飾詞

var 陳述式

function 陳述式

class 陳述式

static 陳述式

概念

變數和常數範圍

型別附註

其他資源

修飾詞