显示实现接口
接口定义了一系列的行为规范,为类型定义一种Can-Do的功能。例如,实现IEnumerable接口定义了GetEnumerator方法,用于获取一个枚举数,该枚举数支持在集合上进行迭代,也就是我们常说的foreach。接口只是定义行为,具体的实现需要由具体类型负责,实现接口的方法又分为隐式实现与显示实现。
一、隐式/显示实现接口方法
简单的说,我们平时“默认”使用的都是隐式的实现方式。例如:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
interface ILog{ void Log();}public class FileLogger : ILog{ public void Log() { Console.WriteLine("记录到文件!"); }} |
隐式实现很简单,通常我们约定接口命名以 I 开头,方便阅读。接口内的方法不需要用public,编译器会自动加上。类型中实现接口的方法只能是public,也可以定义成虚方法,由子类重写。现在看显示实现的方式:
|
1
2
3
4
5
6
7
|
public class EventLogger : ILog{ void ILog.Log() { Console.WriteLine("记录到系统事件!"); }} |
与上面不同的是,方法用了ILog指明,而且没有(也不能有)public或者private修饰符。
除了语法上的不同,调用方式也不同,显示实现只能用接口类型的变量来调用,如:
|
1
2
3
4
5
6
|
FileLogger fileLogger = new FileLogger();fileLogger.Log(); //正确EventLogger eventLogger = new EventLogger(); eventLogger.Log(); //报错ILog log = new EventLogger();log.Log(); //正确 |
二、何时使用
1. c#允许实现多个接口,如果多个接口定义了相同的方法,可以用显示实现的方式加以区分,例如:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
interface ISendable{ void Log();}public class EmailLogger : ILog, ISendable{ void ILog.Log() { Console.WriteLine("ILog"); } void ISendable.Log() { Console.WriteLine("ISendable"); }} |
2. 增强编译时的类型安全和避免值类型装箱
有了泛型,我们自然可以做到编译时的类型安全和避免值类型装箱的操作。但有时候可能没有对应的泛型版本。例如:IComparable(这里只是举例,实际有IComparable<T>)。如:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
interface IComparable{ int CompareTo(object obj);}struct ValueType : IComparable{ private int x; public ValueType(int x) { this.x = x; } public int CompareTo(object obj) { return this.x - ((ValueType)obj).x; }} |
调用:
|
1
2
3
|
ValueType vt1 = new ValueType(1);ValueType vt2 = new ValueType(2);Console.WriteLine(vt1.CompareTo(vt2)); |
由于形参是object,上面的CompareTo会发生装箱;而且无法获得编译时的类型安全,例如我们可以随便传一个string,编译不会报错,等到运行时才抛出InvalidCastException。使用显示实现接口的方式,如:
|
1
2
3
4
5
6
7
8
9
|
public int CompareTo(ValueType vt){ return this.x - vt.x;}int IComparable.CompareTo(object obj){ return CompareTo((ValueType)obj);} |
再次执行上面的代码,就不会发生装箱操作,而且可以获得编译时的类型安全了。但是如果我们用接口变量调用,就会再次发生装箱并丧失编译时的类型安全检测能力。
|
1
2
3
|
IComparable vt1 = new ValueType(1); //装箱ValueType vt2 = new ValueType(2);Console.WriteLine(vt1.CompareTo(vt2)); //再次装箱 |
三、缺点
1. 显示实现只能用接口类型变量调用,会给人的感觉是某类型实现了该接口却无法调用接口中的方法。特别是写成类库给别人调用时,显示实现的接口方法在vs中按f12都不会显示出来。(这点有人在csdn提问过,为什么某个类型可以不用实现接口方法)
2. 对于值类型,要调用显示实现的方法,会发生装箱操作。
3. 无法被子类继承使用。
先看个例子
声明两个接口
- public interface IFg
- {
- int Add();
- void Fei();
- }
- public interface IWf
- {
- int Add();
- void Fei();
- }
Fg类开始继承隐式接口
- public class Fg : IFg, IWf
- {
- public int Add()
- {
- throw new NotImplementedException();
- }
- public void Fei()
- {
- throw new NotImplementedException();
- }
- }
Fg类开始继承显示接口
- public class Fg : IFg, IWf
- {
- int IWf.Add()
- {
- throw new NotImplementedException();
- }
- int IFg.Add()
- {
- throw new NotImplementedException();
- }
- void IWf.Fei()
- {
- throw new NotImplementedException();
- }
- void IFg.Fei()
- {
- throw new NotImplementedException();
- }
- }
第一、显示接口中的方法没有修饰符二隐式接口方法的修饰符为public
第二、显示接口中的方法可以看到从哪里来【通过接口访问,避免访问歧义】,来源相当清晰,隐式接口看不出来源
第三、显示接口会把父级接口中的方法和属性完全继承,隐式接口会过滤冗余的方法
最后用类的方式调用方法会出错,需要用as转换接口类型,平常项目的使用一般都是实现隐式接口,具体要看实际情况设计。

浙公网安备 33010602011771号