黑马程序员--接口

来源:互联网 发布:ubuntu aptitude 编辑:程序博客网 时间:2024/05/22 03:19

---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IO开发S</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------


实现接口的类或结构要与接口的定义严格一致,类和结构可以像类继承基类或结构一样从接口继承,而且可以继承多个接口。当类或结构继承接口时,它继承成员定义但不继承实现。若要实现接口成员,类中的对应成员必须是公共的、非静态的,并且与接口成员具有相同的名称和签名

接口可由方法、属性、事件、索引器或这4种成员类型的任何组合构成。接口不能包含字段。接口成员一定是公共的


接口的声明采用下列格式: 

 修饰符 interface 接口名称 :继承的接口列表  {   接口内容;  }   

其中,除interface 和接口名称,其他的都是可选项

下面是一个C#接口定义的例子

代码如下:

  public interface InterFa

索引器

属性

事件

方法

 } 

接口继承  一个接口可以从一个或多个基接口继承

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication9{    class Program    {        static void Main(string[] args)        {            Person n = new Person();            n.Name = "小丰";            n.开始();            n.交谈();            n.SeHell();            Console.Write("年龄:"+n.age);            Console.ReadKey();        }    }    interface 运动     {        string Name { get; set; }        int this[int indexer] { get; }    }    interface 行为     {        void 交谈();        void SeHell();    }    interface 健身 : 运动     {        void 开始();    }    class Person : 健身,行为    {        public int age = 15;        public string Name { get; set; }        public int this[int indexer]         {            get            {                return age;            }        }        public void 开始()         {            Console.WriteLine("我叫{0}今年{1}岁,我在跑步机上跑步",this.Name,this.age);        }        public void 交谈()         {            Console.WriteLine("我叫{0}你叫什么名字",this.Name);        }        public void SeHell()         {            Console.WriteLine("先生你好啊!");        }    }}



---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------

详细请查看:<a href="http://edu.csdn.net" target="blank">http://edu.csdn.net</a>
原创粉丝点击