C#中接口的理解

来源:互联网 发布:windows exp导出数据库 编辑:程序博客网 时间:2024/06/06 04:06

1.C#中对于接口的定义使用关键字interface

2.接口中的方法都没有方法体。必须在实现它的类中实现方法体

3.接口没有构造函数,也没有字段

4.接口及接口中的方法必须定义为public

5.接口名一般习惯上使用大写的I+自定义名命名

6.接口可以继承接口,如下面代码中的IB接口,继承了IA接口



using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication2{    interface IA//定义一个接口IA    {        void MethodA();    }    interface IB : IA//定义一个接口IB继承另一个接口IA    {        void MethodB();    }    class Bird : IB//一个类Brid,实现了接口IB,IB又继承了IA,则类Brid必须同时实现了IA和IB两个接口中的所有方法    {        public void MethodA()        {            Console.WriteLine("实现了IA接口中的MethodA方法");        }        public void MethodB()        {            Console.WriteLine("实现了IB接口中的MethodB方法");        }    }    class Program    {        static void Main(string[] args)        {            Bird bird = new Bird();//使用父类声明,子类构造(实例化)            bird.MethodA();            bird.MethodB();            Console.ReadLine();        }    }}



强烈向大家推荐一个好网站,http://www.51zxw.net/study.asp?vip=13417828[我要自学网]。

0 0