接口的隐式和显式实现

时间:2021-07-09 03:43:36

While working on a upgrade i happened to come across a code like this.

在进行升级时,碰巧碰到了这样的代码。

interface ICustomization
    {
        IMMColumnsDefinition GetColumnsDefinition();
    }

    class Customization : ICustomization
    {
        private readonly ColumnDefinition _columnDefinition;

        //More code here.

        public ColumnsDefinition GetColumnsDefinition()
        {
            return _columnDefinition;
        }

        ColumnsDefinition ICustomization.GetColumnsDefinition()  //redundant
        {
            return GetColumnsDefinition();            
        }
    }

My question is: Is there any need/use of 'explicit' implementation of interface in this piece of code? Will it create any problem if i remove the method (explicit implementation of interface) that i have marked "redundant" above?

我的问题是:在这段代码中是否需要/使用“显式”接口实现?如果我删除上面标记为“冗余”的方法(显式实现接口)会不会产生任何问题?

PS: I understand that explicit implementation of interface is very important, and it can be used when we need to give access to a method at interface level only, and to use two interface with same signature of method.

PS:我理解接口的显式实现非常重要,当我们需要仅在接口级别访问方法时,可以使用它,并使用具有相同签名方法的两个接口。

2 个解决方案

#1


8  

Yup. Looks redundant.

对。看起来多余。

Calling it via a Customization type of reference and an ICustomization type of reference results in the same behavior. If you wanted the below calls to behave differently, then explicitly implementing the interface would have made sense.

通过自定义类型的引用和ICustomization类型的引用调用它会导致相同的行为。如果您希望以下调用具有不同的行为,那么显式实现该接口将是有意义的。

Customization oVar = new Customization();
oVar.GetColumnsDefinition(); // calls 1st method
ICustomization iVar = obj;
iVar.GetColumnsDefinition(); // calls 2nd method - explicit impl.

You should remove the explicit implementation. However if you remove the other implementation, you will constrain clients such that they can no longer call oVar.GetColumnsDefintion() - they would have to use an interface variable as shown above.

您应该删除显式实现。但是,如果删除其他实现,则会限制客户端,使其无法再调用oVar.GetColumnsDefintion() - 它们必须使用如上所示的接口变量。

#2


4  

For info, the main time you see that specific pattern is when (any one of):

有关信息,您看到特定模式的主要时间是(任何一个):

  • the non-explicit method is virtual or abstract, for subclasses to override
  • 非显式方法是虚方法或抽象方法,用于子类覆盖

  • the signature of the public method is not quite the same, for example the public API has a more specific return type (common for things like IEnumerable[<T>] or ICloneable).
  • 公共方法的签名并不完全相同,例如,公共API具有更具体的返回类型(对于诸如IEnumerable [ ]或ICloneable之类的东西是常见的)。

  • we don't want it to be public, but we want it to be easily callable within the type (without needing a nop-cast)
  • 我们不希望它是公开的,但我们希望它在类型中可以轻松调用(不需要nop-cast)

In this case it does indeed look redundant.

在这种情况下,它确实看起来多余。

#1


8  

Yup. Looks redundant.

对。看起来多余。

Calling it via a Customization type of reference and an ICustomization type of reference results in the same behavior. If you wanted the below calls to behave differently, then explicitly implementing the interface would have made sense.

通过自定义类型的引用和ICustomization类型的引用调用它会导致相同的行为。如果您希望以下调用具有不同的行为,那么显式实现该接口将是有意义的。

Customization oVar = new Customization();
oVar.GetColumnsDefinition(); // calls 1st method
ICustomization iVar = obj;
iVar.GetColumnsDefinition(); // calls 2nd method - explicit impl.

You should remove the explicit implementation. However if you remove the other implementation, you will constrain clients such that they can no longer call oVar.GetColumnsDefintion() - they would have to use an interface variable as shown above.

您应该删除显式实现。但是,如果删除其他实现,则会限制客户端,使其无法再调用oVar.GetColumnsDefintion() - 它们必须使用如上所示的接口变量。

#2


4  

For info, the main time you see that specific pattern is when (any one of):

有关信息,您看到特定模式的主要时间是(任何一个):

  • the non-explicit method is virtual or abstract, for subclasses to override
  • 非显式方法是虚方法或抽象方法,用于子类覆盖

  • the signature of the public method is not quite the same, for example the public API has a more specific return type (common for things like IEnumerable[<T>] or ICloneable).
  • 公共方法的签名并不完全相同,例如,公共API具有更具体的返回类型(对于诸如IEnumerable [ ]或ICloneable之类的东西是常见的)。

  • we don't want it to be public, but we want it to be easily callable within the type (without needing a nop-cast)
  • 我们不希望它是公开的,但我们希望它在类型中可以轻松调用(不需要nop-cast)

In this case it does indeed look redundant.

在这种情况下,它确实看起来多余。