本来是想先说继承的,今天看了一个关于接口和抽象类的文章就顺便先说一下接口。其实在我刚接触编程
的时候,接口就是一个令我很神秘的概念。其实接口和类一样,接口也定义了一系列属性、方法和事件。
但与类不同的是,接口并不提供实现。它们由类来实现,并从类中被定义为单独的实体。接口表示一种约
定,实现接口的类必须严格按其定义来实现接口的每个方面。可能说完这些有些人就会有点晕,在这里只
要记住接口是一种约定就暂时可以了(我这里说的是暂时,以后要深入学习光这一点肯定是不行的)。
今天在这里只想说一个问题就是如何显式的实现接口成员,可能会有人说我的这个系列根本就称不上系列
,根本就没有什么体系。对,确实我写的这些没有什么体系,也不是按什么顺序写的,但是本人一向认为
:无论是学习什么,并不是所有学习都是按照顺序按照体系学习的,而且即使你按体系来学习了,也不一
定能学会。所以我认为学习只要能学会一点就学一点,哪怕学的是支离破碎的,等以后再学到相关内容时
,你定然会恍然大悟。我认为这样学会的东西定然比你按照体系一步一步学到的东西记得牢,体会的深。
书归正转,用实例说明如何显式的实现接口成员,并在最后补充说明如何通过继承来实现接口成员。先看
个例子:
先定义两个接口,而且这两个接口有相同的成员
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public interface person
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
string name();
4
int age();
5
}
6
public interface student
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
8
string name();
9
int age();
10
}
然后定义一个类继承这两个接口
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
class haha:person,student
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
string myname;
4
int myage;
5![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
public haha(string name, int age)
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
myname = name;
9
myage = age;
10
}
11
//实现接口方法
12
string student.name()
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
return myname;
15
}
16
int student.age()
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
return myage;
19
}
20
int person.age()
21![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
22
return myage;
23
}
24
string person.name()
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
return myname;
27
}
28![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
}
实例化上面这个类并实例化一个接口然后输出
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
haha myhaha = new haha("Myhaha", 20);
person myperson = (person)myhaha;
Console.WriteLine("My name is: {0}",myperson.name()); //这里不可以企图直接使用
//myhaha.name来输出,因为显式实现的接口成员不能从类实例访//问,这点很重要喔。
Console.WriteLine("My age is: {0}", myperson.age());
如果希望通过类实例直接访问成员就要在类定义时正常实现成员,如本例中
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public string name()
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
3
return myname;
4
}
5
public int age()
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
7
return myage;
8
}
完整代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
namespace jiekou
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
7
public interface person
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
string name();
10
int age();
11
}
12
public interface student
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
string name();
15
int age();
16
}
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
class haha:person,student
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
string myname;
21
int myage;
22![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
public haha(string name, int age)
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
myname = name;
26
myage = age;
27
}
28
//实现接口方法
29
string student.name()
30![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
31
return myname;
32
}
33
int student.age()
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
return myage;
36
}
37
int person.age()
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
39
return myage;
40
}
41
string person.name()
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
return myname;
44
}
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
//正常实现成员方法
47
public string name()
48![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
49
return myname;
50
}
51
public int age()
52![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
53
return myage;
54
}
55
}
56
class Program
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
static void Main(string[] args)
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
60
haha myhaha = new haha("Myhaha", 20);
61
person myperson = (person)myhaha;
62
Console.WriteLine("My name is: {0}",myperson.name());
63
Console.WriteLine("My age is: {0}", myperson.age());
64![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
Console.WriteLine();
66
Console.WriteLine("My name is: {0}", myhaha.name());
67
Console.WriteLine("My age is: {0}", myhaha.age());
68
Console.ReadKey();
69![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
}
71
}
72
}
73![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)