舉例如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | interface Demo { void DoSomething(); } interface Demo2 : Demo { void DoSomething2( string s); } class Demo3 : Demo2 { void Demo.DoSomething() { System.Console.WriteLine( "There is no spoon." ); } void Demo2.DoSomething2( string s) { System.Console.WriteLine(s); } static void Main() { Demo2 d = new Demo3(); d.DoSomething(); d.DoSomething2( "Free your mind." ); } } /* 《程式語言教學誌》的範例程式 檔名:interface2.cs 功能:示範 C# 程式 作者:張凱慶 時間:西元 2013 年 6 月 */ |
Demo2 繼承自 Demo 介面
5 6 7 | interface Demo2 : Demo { void DoSomething( string s); } |
Demo3 類別 (class) 實作 Demo2 ,因此 Demo 與 Demo2 宣告的方法 (method) 都要定義
9 | class Demo3 : Demo2 { |
這裡須注意, DoSomething() 是 Demo 宣告的, DoSomething2() 則是 Demo2 宣告的
10 11 12 13 14 15 16 | void Demo.DoSomething() { System.Console.WriteLine( "There is no spoon." ); } void Demo2.DoSomething2( string s) { System.Console.WriteLine(s); } |
編譯執行,結果如下

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