static 類別也無法初始化為物件。
舉例如下
class Demo {
public static void DoSomething() {
System.Console.WriteLine("Choice. The problem is choice.");
}
}
public class DemoTest {
static void Main() {
Demo.DoSomething();
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:class09.cs
功能:示範 C# 程式
作者:張凱慶
時間:西元 2013 年 6 月 */這裡可以看到是直接用 Demo 呼叫 static 的 DoSomething() 方法
Demo.DoSomething();
編譯執行,結果如下

下例示範 static 類別
static class Demo {
public static void DoSomething() {
System.Console.WriteLine("There is no spoon.");
}
public static void DoSomething2() {
System.Console.WriteLine("Free your mind.");
}
}
public class DemoTest {
static void Main() {
Demo.DoSomething();
Demo.DoSomething2();
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:class10.cs
功能:示範 C# 程式
作者:張凱慶
時間:西元 2013 年 6 月 */編譯執行,結果如下

| 中英文術語對照 | |
|---|---|
| 關鍵字 | keyword |
| 類別 | class |
您可以繼續參考
類別
相關目錄
回 C# 快速導覽
回 C# 教材
回首頁
參考資料
Standard ECMA-334 C# Language Specification
msdn: 靜態類別和靜態類別成員 (C# 程式設計手冊)
沒有留言:
張貼留言