声明类成员属于类,而不属于类的实例。
static statement
- statement
必选。类成员定义。
static 修饰符指明成员属于类本身而不属于类的实例。即使创建了类的多个实例,给定应用程序中只存在 static 成员的一个副本。您只能通过对类的引用(而不是对实例的引用)来访问 static 成员。但是,在类成员声明中,可以通过 this 对象来访问 static 成员。
类的成员可以使用 static 修饰符来标记。类、接口和接口的成员不能采用 static 修饰符。
不能将 static 修饰符与任何继承修饰符(abstract 和 final)或版本安全修饰符(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要求请参见
参考
expando 修饰符var 语句
function 语句
class 语句
static 语句
概念
变量和常数的范围类型批注