C#中的接口

接口的注意事项:
   1)接口是实现,抽象方法是继承
   2)实现接口的类中可以有普通方法
   3)定义接口使用关键字Internet
   4)实现接口无关键字
   5)一次可以实现多个接口

示例代码:
    //接口1
   public interface IFly
    {
       void Fly();
    }


    //接口2
   public interface IRun
    {
       void Run();
    }

 

 

//实现接口
   public class Plan:IFly,IRun
    {
       public void Fly()
       {
           Console.WriteLine("plan fly");
       }
       public void  Run() 
       {
           Console.WriteLine("RUN");
       }
       public void Eat()  //普通方法
       {
           Console.WriteLine("eat");
       }
    }

 

 //实现接口
   public class Brid:IFly
    {
       public void Fly()
       {
           Console.WriteLine("fly");
       }
    }

 

测试类:

static void Main(string[] args)
        {
            List<IFly> list = new List<IFly>()
            {
                new Brid(),
                new Plan()
            };
            foreach (IFly item in list)
            {
                item.Fly();
            }
            Console.ReadKey();
        }

 

posted @ 2017-06-27 19:52  努力奋斗吧  阅读(1394)  评论(0编辑  收藏  举报