C#接口实例详解(1)s
----C#接口实例详解----
介绍C#中的接口提供了一种实现运行时的多态。通过接口可以使用相同接口的引用来访问实现相同接口的不同类的方法,其实是使用虚方法通过相同的引用调用相同基础的不同的类。在开始前先使用简单的短类例子来解释接口的概念,下面的简短的例子显示接口的样子。
P1.cs
程序代码:
class Demo { public static void Main() { System.Console.WriteLine("Hello Interfaces"); } } interface abc { }
输出:
Hello Interfaces |
编译运行上面的程序运行程序并显示出期望的结果。这段程序包含一个Demo类程序入门Main()方法中打印“Hello Interfaces”。在上面的程序中还定义了接口abc。abc接口是空的,可以在接口中添加一些元素。
P2.cs
程序代码:
class Demo { public static void Main() { System.Console.WriteLine("Hello Interfaces"); } } interface abc { int x; }
输出: P2.cs(11,3): error CS0525: Interfaces cannot contain fields |
错误!在C#的接口中不能包含字段例如变量。上面的程序在接口abc中声明了一个整型变量x。编译将会出错。
P3.cs
程序代码:
class Demo { public static void Main() { System.Console.WriteLine("Hello Interfaces"); } } interface abc { void xyz() { System.Console.WriteLine("In xyz"); } }
输出: P3.cs(11,8): error CS0531: 'abc.xyz()': interface members cannot have a definition |
P4.cs
程序代码:
class Demo { public static void Main() { System.Console.WriteLine("Hello Interfaces"); } } interface abc { void xyz(); }
输出: Hello Interfaces |
P5.cs
程序代码:
class Demo : abc { public static void Main() { System.Console.WriteLine("Hello Interfaces"); } } interface abc { void xyz(); }
输出: P4.cs(1,7): error CS0535: 'Demo' does not implement interface member 'abc.xyz()'P4.cs(11,8): (Location of symbol related to previous error) |
P6.cs
程序代码:
class Demo : abc { public static void Main() { System.Console.WriteLine("Hello Interfaces"); } void xyz() { System.Console.WriteLine("In xyz"); } } interface abc { void xyz(); }
输出: a.cs(1,7): error CS0536: 'Demo' does not implement interface member 'abc.xyz()'.'Demo.xyz()' is either static, not public, or has the wrong return type.a.cs(16,8): (Location of symbol related to previous error)a.cs(7,8): (Location of symbol related to previous error) |
P7.cs
好的!上面的代码编译运行成功产生预期的输出结果。正如前面提及的接口可以调用实现相同的接口的不同的类。因此,需要不同的实现相同接口的类。在上面的代码中类Demo实现了接口abc。下面让另一个类Sample也实现接口abc。
程序代码:
class Demo : abc { public static void Main() { Demo demo = new Demo(); System.Console.WriteLine("Hello Interfaces"); demo.xyz(); } public void xyz() { System.Console.WriteLine("In xyz"); } } interface abc { void xyz(); }
输出: Hello InterfacesIn xyz |
P8.cs
程序代码:
class Demo : abc { public static void Main() { System.Console.WriteLine("Hello Interfaces"); Demo refDemo = new Demo(); refDemo.xyz(); Sample refSample = new Sample(); refSample.xyz(); } public void xyz() { System.Console.WriteLine("In Demo :: xyz"); } } interface abc { void xyz(); } class Sample : abc { public void xyz() { System.Console.WriteLine("In Sample :: xyz"); } }
输出: In Demo :: xyzIn Sample :: xyz |
P9.cs
程序代码:
class Demo : abc { public static void Main() { System.Console.WriteLine("Hello Interfaces"); abc refabc = null; refabc = new Demo(); refabc.xyz(); refabc = new Sample(); refabc.xyz(); } public void xyz() { System.Console.WriteLine("In Demo :: xyz"); } } interface abc { void xyz(); } class Sample : abc { public void xyz() { System.Console.WriteLine("In Sample :: xyz"); } }
输出: In Demo :: xyzIn Sample :: xyz |
P10.cs
程序代码:
class Demo : abc { public static void Main() { abc[] refabc = { new Demo(), new Sample() }; for(int i = 0; i <= 1; i++) refabc[i].xyz(); } public void xyz() { System.Console.WriteLine("In Demo :: xyz"); } } interface abc { void xyz(); } class Sample : abc { public void xyz() { System.Console.WriteLine("In Sample :: xyz"); } }
输出: In Demo :: xyzIn Sample :: xyz |
P11.cs
程序代码:
class Demo : abc, def { public static void Main() { System.Console.WriteLine("Hello Interfaces"); abc refabc = new Demo(); refabc.xyz(); } public void xyz() { System.Console.WriteLine("In xyz"); } public void pqr() { System.Console.WriteLine("In xyz"); } } interface abc { void xyz(); } interface def { void pqr(); }
输出: Hello InterfacesIn xyz |
P12.cs
程序代码:
class Demo : abc, def { public static void Main() { System.Console.WriteLine("Hello Interfaces"); abc refabc = new Demo(); refabc.xyz(); refabc.pqr(); } public void xyz() { System.Console.WriteLine("In xyz"); } public void pqr() { System.Console.WriteLine("In xyz"); } } interface abc { void xyz(); } interface def { void pqr(); }
输出:
P11.cs(9,5): error CS0117: 'abc' does not contain a definition for 'pqr' |
错误!尝试通过定义为接口abc类型的变量refabc的Demo实例来访问pqr()方法,在接口abc中包含了函数xyz()的原型但没有包含pqr()方法原型。可以通过类型为接口def的Demo实例来方法pqr()方法因为接口def包含方法pqr()的原型。