zl程序教程

您现在的位置是:首页 >  其他

当前栏目

显示实现接口

接口 实现 显示
2023-09-11 14:14:39 时间

  接口定义了一系列的行为规范,为类型定义一种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. 无法被子类继承使用。

 

 

 

先看个例子

声明两个接口

 

[csharp] view plain copy
 
  1. public  interface IFg  
  2. {  
  3.     int Add();  
  4.     void Fei();  
  5. }  
  6.   
  7. public interface IWf  
  8. {  
  9.     int Add();  
  10.     void Fei();  
  11. }  

Fg类开始继承隐式接口

 

[csharp] view plain copy
 
  1. public class Fg : IFg, IWf  
  2. {  
  3.     public int Add()  
  4.     {  
  5.         throw new NotImplementedException();  
  6.     }  
  7.   
  8.     public void Fei()  
  9.     {  
  10.         throw new NotImplementedException();  
  11.     }  
  12. }  

Fg类开始继承显示接口

 

[csharp] view plain copy
 
  1. public class Fg : IFg, IWf  
  2. {  
  3.     int IWf.Add()  
  4.     {  
  5.         throw new NotImplementedException();  
  6.     }  
  7.   
  8.     int IFg.Add()  
  9.     {  
  10.         throw new NotImplementedException();  
  11.     }  
  12.   
  13.     void IWf.Fei()  
  14.     {  
  15.         throw new NotImplementedException();  
  16.     }  
  17.   
  18.     void IFg.Fei()  
  19.     {  
  20.         throw new NotImplementedException();  
  21.     }  
  22. }  
看到两个接口的实现后可总结:

第一、显示接口中的方法没有修饰符二隐式接口方法的修饰符为public

第二、显示接口中的方法可以看到从哪里来【通过接口访问,避免访问歧义】,来源相当清晰,隐式接口看不出来源

第三、显示接口会把父级接口中的方法和属性完全继承,隐式接口会过滤冗余的方法

 

最后用类的方式调用方法会出错,需要用as转换接口类型,平常项目的使用一般都是实现隐式接口,具体要看实际情况设计。

 

 

版权声明:本文为博主原创文章,未经博主允许不得转载,如果转载请写明出处。 http://blog.csdn.net/w200221626/article/details/52033192