欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#隐式/显示实现接口方法详解

程序员文章站 2022-09-07 11:48:27
接口定义了一系列的行为规范,为类型定义一种can-do的功能。例如,实现ienumerable接口定义了getenumerator方法,用于获取一个枚举数,该枚举数支持在集...

接口定义了一系列的行为规范,为类型定义一种can-do的功能。例如,实现ienumerable接口定义了getenumerator方法,用于获取一个枚举数,该枚举数支持在集合上进行迭代,也就是我们常说的foreach。接口只是定义行为,具体的实现需要由具体类型负责,实现接口的方法又分为隐式实现与显示实现。

一、隐式/显示实现接口方法

  简单的说,我们平时“默认”使用的都是隐式的实现方式。例如:

interface ilog
{
  void log();
}
 
public class filelogger : ilog
{
  public void log()
  {
    console.writeline("记录到文件!");
  }
}

  隐式实现很简单,通常我们约定接口命名以 i 开头,方便阅读。接口内的方法不需要用public,编译器会自动加上。类型中实现接口的方法只能是public,也可以定义成虚方法,由子类重写。现在看显示实现的方式:

public class eventlogger : ilog
{
  void ilog.log()
  {
    console.writeline("记录到系统事件!");
  }
}

  与上面不同的是,方法用了ilog指明,而且没有(也不能有)public或者private修饰符。

  除了语法上的不同,调用方式也不同,显示实现只能用接口类型的变量来调用,如:

filelogger filelogger = new filelogger();
filelogger.log(); //正确
eventlogger eventlogger = new eventlogger();      
eventlogger.log(); //报错
ilog log = new eventlogger();
log.log(); //正确

二、何时使用

  1. c#允许实现多个接口,如果多个接口定义了相同的方法,可以用显示实现的方式加以区分,例如:

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>)。如:

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;
  }
} 

  调用:

valuetype vt1 = new valuetype(1);
valuetype vt2 = new valuetype(2);
console.writeline(vt1.compareto(vt2));

  由于形参是object,上面的compareto会发生装箱;而且无法获得编译时的类型安全,例如我们可以随便传一个string,编译不会报错,等到运行时才抛出invalidcastexception。使用显示实现接口的方式,如:

public int compareto(valuetype vt)
{
  return this.x - vt.x;
}
 
int icomparable.compareto(object obj)
{
  return compareto((valuetype)obj);
} 

  再次执行上面的代码,就不会发生装箱操作,而且可以获得编译时的类型安全了。但是如果我们用接口变量调用,就会再次发生装箱并丧失编译时的类型安全检测能力。

icomparable vt1 = new valuetype(1); //装箱
valuetype vt2 = new valuetype(2);
console.writeline(vt1.compareto(vt2)); //再次装箱

三、缺点

  1. 显示实现只能用接口类型变量调用,会给人的感觉是某类型实现了该接口却无法调用接口中的方法。特别是写成类库给别人调用时,显示实现的接口方法在vs中按f12都不会显示出来。(这点有人在csdn提问过,为什么某个类型可以不用实现接口方法)

  2. 对于值类型,要调用显示实现的方法,会发生装箱操作。

  3. 无法被子类继承使用。

以上就是c#显示实现接口的全部内容,希望对大家的学习有所帮助。