今天就来简单的介绍下接口的基本概念及用法:
1、接口的概念及声明
接口是一种用来定义程序的协议,它描述可属于任何类或结构的一组相关行为。接口可有方法、属性、事件和索引器或这四种成员的任何组合类型,但不能包含字段。
那么接口具有哪些特点呢?
- 接口类似于抽象基类:继承接口的任何非抽象类型都必须实现接口的所有成员(说明:如类A继承接口B,那么A中必须实现B中定义的属性,方法等)。
- 不能直接实例化接口
- 接口可以包含事件、索引器、方法和属性
- 接口不包含方法的实现
- 类和接口可以从多个接口继承
- 接口自身可以继承多个接口
在声明接口时除了Interface和接口名称是必须的,其他都是可选项。另可使用new、public、protected、intenal和private等修饰符实现接口,但接口成员必须是公共的。
2、接口的实现与继承
声明实现接口的类时,需要在基类列表中包含类所实现的接口的名称。
下面举个小例子来探讨下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| using System; using System.Collections.Generic; using System.Text;
namespace _ { interface ImyInterface { string ID { get; set; } string Name { get; set; } void ShowInfo(); } class Program:ImyInterface { string id = ""; string name = ""; public string ID { get { return id; } set { id = value; } } public string Name { get { return name; } set { name = value; } } public void ShowInfo() { Console.WriteLine("编号\t 姓名"); Console.WriteLine(ID + "\t " + Name); } static void Main(string[] args) { Program program = new Program(); ImyInterface imyinterface = program; imyinterface.ID = "TM"; imyinterface.Name = "C# 2.0从入门到应用开发"; imyinterface.ShowInfo(); } } }
|
上面的这个例子是一个简单的一个类继承一个接口,如要实现接口类中对应的成员必须是公共的、非静态的,并且与接口成员具有相同的名称和签名。并且接口的实例化不能像类那样Program program = new Program();其中上述程序中红色部分即为接口的实例化(使用派生类对象实例化接口)。
单一继承时这样的,那么多重继承呢?OK,我们来看下个例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| using System; using System.Collections.Generic; using System.Text;
namespace _ { interface IPeople { string Name { get; set; } string Sex { get; set; } } interface ITeacher:IPeople { void teach(); } interface IStudent:IPeople { void study(); } class Program:IPeople,ITeacher,IStudent { string name = ""; string sex = ""; public string Name { get { return name; } set { name = value; } } public string Sex { get { return sex; } set { sex = value; } } public void teach() { Console.WriteLine(Name + " " + Sex + " 教师"); } public void study() { Console.WriteLine(Name + " " + Sex + " 学生"); } static void Main(string[] args) { Program program = new Program(); ITeacher iteacher = program; iteacher.Name = "TM"; iteacher.Sex = "男"; iteacher.teach(); IStudent istudent = program; istudent.Name = "C#"; istudent.Sex = "男"; istudent.study(); } } }
|
上述的多重继承中说明了,在派生类中必须实现所继承的接口中的所有方法。OK,单一继承和多重继承都有了了解之后,是不是没有其他的需要了解的呢?试想一下,如果在一个类A继承自接口B和C,并且在B和C中包含具有相同签名的成员,那么在类中实现该成员将导致两个接口都使用该成员作为他们的实现,然而,如果两个接口成员实现不同的功能,那么将会导致一个接口的成员实现不正确或两个接口的成员实现都不正确,这个时候我们应该如何处理呢?我们可以显示的实现接口成员,即创建一个仅通过接口调用并且特定于该接口的类成员。
下面也是一个通过一个实例来说明
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| using System; using System.Collections.Generic; using System.Text;
namespace _ { interface ImyInterface1 { int Add(); } interface ImyInterface2 { int Add(); } class myClass : ImyInterface1, ImyInterface2 { int ImyInterface1.Add() { int x = 3; int y = 5; return x + y; } int ImyInterface2.Add() { int x = 3; int y = 5; int z = 7; return x + y + z; } } class Program { static void Main(string[] args) { myClass myclass = new myClass(); ImyInterface1 imyinterface1 = myclass; Console.WriteLine(imyinterface1.Add()); ImyInterface2 imyinterface2 = myclass; Console.WriteLine(imyinterface2.Add()); } } }
|
上面的实例中在Myclass类中,通过两个显示接口成员的方法分别实现了两个接口中的Add方法,在实例化不同的接口后,调用相应的方法实现输出结果。