首页 > 代码库 > Interface/接口

Interface/接口

1. 类和结构能够实现接口

2. 接口声明包含如下四种类型:属性、方法、事件和索引;这些函数声明不能包含任何实现代码,而在每一个成员的主体后必须使用分号

3. 继承接口的类或结构必须实现接口中的所有成员

4. 显示接口的实现注意下面的代码

class Program    {        static void Main(string[] args)        {            SampleClass_1 sc = new SampleClass_1();            IControl ctr1 = (IControl)sc;            ISurface srfc = (ISurface)sc;            // 调用一样的方法,继承            sc.Paint();            ctr1.Paint();            srfc.Paint();            Console.WriteLine();            //显式接口实现            SampleClass_2 obj = new SampleClass_2();            IControl c = (IControl)obj;            ISurface s = (ISurface)obj;            obj.Paint();            c.Paint();            s.Paint();            Console.ReadKey();        }    }    public interface IControl // 接口可以有访问修饰符    {        void Paint();      //成员不允许有任何访问修饰符,默认public        int P { get; }    }    public interface ISurface    {        void Paint();    }    class SampleClass_1:IControl,ISurface    {        //IControl.Paint和ISurface.Paint都会调用这个方法        public void Paint()        {            Console.WriteLine("Paint method in SampleClass");        }        private int _value=http://www.mamicode.com/10;"IControl.Paint");        }        void ISurface.Paint()        {            Console.WriteLine("ISurface.Paint");        }        public void Paint()        {            Console.WriteLine("SampleClass_2.Paint");        }        private int _value=http://www.mamicode.com/10;>

  

Interface/接口