20-C#筆記-接口
阿新 • • 發佈:2018-05-22
odt tro brush ren 實現接口 har .com ace console
# 1 接口的使用示例
使用interface,關鍵字
接口的實現和使用,和繼承類似。
在使用之前,要實現接口。
using System; interface IMyInterface { // 接口成員 void MethodToImplement(); } class InterfaceImplementer : IMyInterface { static void Main() { InterfaceImplementer iImp = new InterfaceImplementer(); iImp.MethodToImplement(); } public void MethodToImplement() { Console.WriteLine("MethodToImplement() called."); } }
# 2 接口的繼承
在繼承接口的類中,要實現所有的接口。
using System; interface IParentInterface { void ParentInterfaceMethod(); } interface IMyInterface : IParentInterface { void MethodToImplement(); } class InterfaceImplementer : IMyInterface { static void Main() { InterfaceImplementer iImp = new InterfaceImplementer(); iImp.MethodToImplement(); iImp.ParentInterfaceMethod(); } public void MethodToImplement() { Console.WriteLine("MethodToImplement() called."); } public void ParentInterfaceMethod() { Console.WriteLine("ParentInterfaceMethod() called."); } }
參考:
http://www.runoob.com/csharp/csharp-interface.html
20-C#筆記-接口