C#中显/隐式实现接口及其访问方法
1.隐式实现的接口
结果:
Pain method is called!
Pain method is called!
说明:从实例中我们可以看到用隐式实现的接口既可以通过类来访问,也可以通过接口来访问!
2.显式实现的接口
结果:
说明:从实例中我们可以看到用显式实现的接口只能通过接口来访问,如果试图通过类来访问会出错:““ConsoleApplication1.EditBox”并不包含“Paint”的定义。”
3.同时用显/隐式实现接口会怎么样?
结果:
Pain method is called!
IControl.Pain method is called!
说明:当同时用显/隐式实现接口时,显式才是真正的接口实现方法!
4.结论
在多数情况下,我们都是用隐式来实现接口,此时既可以通过类来访问,又可以通过接口来访问,而通过显式实现的接口则只能通过接口来访问,总结一下就是:当显式实现方式存在时,隐式实现方式就失效了。但这不能表示显式实现方式就不好,当一个类实现的多个接口中具有相同的方法时,用显式方式来专门实现某个接口的方法时就显得非常有用!
5.参考文章
[1] C#中的接口:http://zhenyulu.cnblogs.com/archive/2006/04/18/377705.html
1
interface IControl
2
{
3
void Paint();
4
}
5
public class EditBox : IControl
6
{
7
public void Paint()
8
{
9
Console.WriteLine("Pain method is called!");
10
}
11
}
12
class Test
13
{
14
static void Main()
15
{
16
EditBox editbox = new EditBox();
17
editbox.Paint();
18
((IControl)editbox).Paint();
19
Console.ReadKey();
20
}
21
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
结果:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
说明:从实例中我们可以看到用隐式实现的接口既可以通过类来访问,也可以通过接口来访问!
2.显式实现的接口
1
interface IControl
2
{
3
void Paint();
4
}
5
public class EditBox : IControl
6
{
7
void IControl.Paint()
8
{
9
Console.WriteLine("IControl.Pain method is called!");
10
}
11
}
12
class Test
13
{
14
static void Main()
15
{
16
EditBox editbox = new EditBox();
17
//editbox.Paint();//通过类访问会出错
18
((IControl)editbox).Paint();
19
Console.ReadKey();
20
}
21
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
12
![](/Images/OutliningIndicators/None.gif)
13
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
结果:
1
IControl.Pain method is called!
![](/Images/OutliningIndicators/None.gif)
说明:从实例中我们可以看到用显式实现的接口只能通过接口来访问,如果试图通过类来访问会出错:““ConsoleApplication1.EditBox”并不包含“Paint”的定义。”
3.同时用显/隐式实现接口会怎么样?
1
interface IControl
2
{
3
void Paint();
4
}
5
public class EditBox : IControl
6
{
7
void IControl.Paint()
8
{
9
Console.WriteLine("IControl.Pain method is called!");
10
}
11
public void Paint()
12
{
13
Console.WriteLine("Pain method is called!");
14
}
15
}
16
class Test
17
{
18
static void Main()
19
{
20
EditBox editbox = new EditBox();
21
editbox.Paint();
22
((IControl)editbox).Paint();
23
Console.ReadKey();
24
}
25
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](/Images/OutliningIndicators/InBlock.gif)
4
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/InBlock.gif)
8
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![](/Images/OutliningIndicators/InBlock.gif)
10
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
11
![](/Images/OutliningIndicators/InBlock.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
16
![](/Images/OutliningIndicators/None.gif)
17
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
结果:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
说明:当同时用显/隐式实现接口时,显式才是真正的接口实现方法!
4.结论
在多数情况下,我们都是用隐式来实现接口,此时既可以通过类来访问,又可以通过接口来访问,而通过显式实现的接口则只能通过接口来访问,总结一下就是:当显式实现方式存在时,隐式实现方式就失效了。但这不能表示显式实现方式就不好,当一个类实现的多个接口中具有相同的方法时,用显式方式来专门实现某个接口的方法时就显得非常有用!
5.参考文章
[1] C#中的接口:http://zhenyulu.cnblogs.com/archive/2006/04/18/377705.html
------------------------------------------------
dnawo:天才在于勤奋,知识在于积累!