舉例如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public abstract class Demo { public abstract void DoSomething(); } class Demo2 : Demo { public override void DoSomething() { System.Console.WriteLine( "There is no spoon." ); } static void Main() { Demo2 d = new Demo2(); d.DoSomething(); } } /* 《程式語言教學誌》的範例程式 檔名:class19.cs 功能:示範 C# 程式 作者:張凱慶 時間:西元 2013 年 6 月 */ |
Demo 為 abstruct 類別,其內的方法也都得宣告為 abstruct
1 2 3 | public abstract class Demo { public abstract void DoSomething(); } |
Demo2 繼承 Demo ,使用 override 改寫 Demo 宣告的方法
6 7 8 | public override void DoSomething() { System.Console.WriteLine( "There is no spoon." ); } |
編譯執行,結果如下

中英文術語對照 | |
---|---|
類別 | class |
父類別 | superclass |
方法 | method |
您可以繼續參考
類別
相關目錄
回 C# 快速導覽
回 C# 教材
回首頁
參考資料
Standard ECMA-334 C# Language Specification
msdn: abstract (C# 參考)
沒有留言:
張貼留言