暮色呼如
接口用于描述已实现的功能。因此,您可以将实现同一接口的多个对象视为该接口的类型。例如:public interface IMyInterface{ public void DoFirst(); public int DoSecond();}public class A : IMyInterface{ //class has to implement DoFirst and DoSecond public void DoFirst(){ Console.WriteLine("Blubb1"); } public int DoSecond(){ Console.WriteLine("Blubb2"); return 2; }}public class B : IMyInterface{ //class has to implement DoFirst and DoSecond public void DoFirst(){ Console.WriteLine("Blibb1"); } public int DoSecond(){ Console.WriteLine("Blibb2"); return 4; }}这些类以几种方式实现Interface。但是您可以将它们用作IMyInterface。例如:public static void DoMethodsInInterface(IMyInterface inter){ inter.DoFirst(); inter.DoSecond();}public static void main(){ DoMethodsInInterface(new A()); DoMethodsInInterface(new B()); //Or use it in a List List<IMyInterface> interlist = new List<IMyInterface>(); interlist.Add(new A()); interlist.Add(new B()); foreach(IMyInterface inter in interlist){ inter.DoFirst(); }}我希望这可以弄清楚为什么接口有用。