C#Interface 接口

为了解决多继承的问题而诞生的一种特殊的“类”。 接口里面只能写方法的定义和属性的定义。
       在接口中的方法定义前面不能含有public
       子类在实现接口的方法是,不能使用override关键字。

下面我们一起来学习吧..

View Code
 1 using System;
 2  public class studyInterface
 3  {
 4      public static void Main()
 5      {
 6          Ustorge u = new Ustorge();
 7          KnowUSB(u);
 8          Iphone p = new Iphone();
 9          KnowUSB(p);
10          MP3 mp3 = new MP3();
11          KnowUSB(mp3);
12         
13      }
14      //主要程序
15      public static void KnowUSB(IUSB usb)
16      {
17        usb.KnowType();
18      }
19     
20  }
21 
22 public interface IUSB
23 {
24     void KnowType(); //不能有修饰符public
25 }
26 /*--------------------实现接口--------------------------------*/
27 //和抽象类一样,必须一一实现
28 public class Ustorge :IUSB
29 {
30     public void KnowType()
31     {
32       Console.WriteLine("可识别USB对象:U盘");
33     }
34 }
35 public class Iphone :IUSB
36 {
37     public void KnowType()
38     {
39       Console.WriteLine("可识别USB对象:手机USB接口");
40     }
41 }
42 public class MP3 :IUSB
43 {
44     public void KnowType()
45     {
46       Console.WriteLine("可识别USB对象:MP3");
47     }
48 }
原文地址:https://www.cnblogs.com/QLJ1314/p/interface.html