问题描述
虽然工作的一个升级我碰巧遇到一个code这样的。
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();
}
}
我的问题是:有没有在这片code任何需要/使用的显性实现的接口?它会产生任何问题,如果我删除的方法(显式实现接口),我已经标有多余上面?
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:据我了解,明确实现的接口是非常重要的,它可以当我们需要给访问的方法只有接口电平,并使用两个接口的方法相同的签名中使用
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.
推荐答案
是啊。看起来是多余的。
Yup. Looks redundant.
通过参考一个定制类型和参考结果相同的行为的ICustomization类型调用它。如果你想下面的调用不同的行为,则显式地实现该接口将是有意义的。
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.
Customization oVar = new Customization();
oVar.GetColumnsDefinition(); // calls 1st method
ICustomization iVar = obj;
iVar.GetColumnsDefinition(); // calls 2nd method - explicit impl.
您应该删除了明确的实现。不过,如果你删除其它的实现,你会限制客户端,以便他们可以不再调用oVar.GetColumnsDefintion() - 他们的有无以的使用接口变量,如上图所示。
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.
这篇关于隐式和显式实现的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!