为什么要使用显式接口实现调用一个保护方法? [英] Why to Use Explicit Interface Implementation To Invoke a Protected Method?
问题描述
当在 CodePlex上浏览ASP.NET MVC的源代码,我觉得是很常见的有一个类实现显式接口。然后明确实施方法/属性调用另外一个受保护的虚拟方法/具有相同名称的属性。
When browsing ASP.NET MVC source code in codeplex, I found it is common to have a class explicitly implementing interface. The explicitly implemented method/property then invoke another "protected virtual" method/property with same name.
例如,
public class MvcHandler : IHttpHandler, IRequiresSessionState
{
protected virtual bool IsReusable
{
get
{
return false;
}
}
bool IHttpHandler.IsReusable
{
get
{
return IsReusable;
}
}
}
我现在知道什么是这种编程的好处。对于我来说,我更愿意只含蓄地实现该接口的IHttpHandler。
I'm now sure what's the benefit of this kind of programming. For me, I prefer to just implicitly implement the interface IHttpHandler.
我想笔者只是不想 MvcHandler 有一个公共财产< STRONG> IsResuable 。该物业的 IsReusable 只能用时 MvcHandler 被视为的IHttpHandler 实例。不过,我不知道作者为什么这样什么样的方式。
I guess the author just don't want MvcHandler has a public property IsResuable. The property IsReusable can only be used when instance of MvcHandler is treated as a IHttpHandler. Still, I'm not sure why the author what this way.
任何人都知道这种风格的界面实现更多的好处?
Anybody know more benefits about this style of interface implementation?
推荐答案
哦,不是具体到MVC,但这种方法可以让你的保持核心公共API清洁。它也是有用的,如果有以往一个/不同接口的风险等具有相同的名称和放大器;签名,但不同的含义。在现实中,这是罕见的。
Well, not specific to MVC, but this approach allows you to keep the core public API clean. It is also useful if there is ever a risk of different interfaces / etc having the same name & signature, but different meaning. In reality this is rare.
它还允许你提供你想要的返回类型在子类中改变一个实现:
It also allows you to provide an implementation where you want the return type to change in subclasses:
( ICloneable
选择了简单 - 不要让挂了这样的事实,这是一个不好界定的界面...一个更好的例子本来事情像的DbCommand
等,这些做到这一点 - 但就是很难在很短的例子来说明)
(ICloneable
chosen for simplicity - don't get hung up on the fact that it is a poorly defined interface... a better example would have been things like DbCommand
etc, which do this - but that is harder to show in a short example)
class Foo : ICloneable
{
public Foo Clone() { return CloneCore(); }
object ICloneable.Clone() { return CloneCore(); }
protected virtual Foo CloneCore() { ... }
}
class Bar : Foo
{
protected override Foo CloneCore() { ... }
public new Bar Clone() { return (Bar)CloneCore(); }
}
如果我们使用一个公用虚拟方法,我们就不能到覆盖
它的和的使用新
中的基类,因为你不允许一举两得:
If we had used a public virtual method, we wouldn't be able to override
it and use new
in the base-class, as you aren't allowed to do both:
class A
{
public virtual A SomeMethod() { ... }
}
class B : A
{
public override A SomeMethod() { ... }
//Error 1 Type 'B' already defines a member called 'SomeMethod' with the same parameter types
public new B SomeMethod() { ... }
}
使用受保护的虚拟方式,任何用法:
Using the protected virtual approach, any usage:
- Foo.Clone()
- Bar.Clone()
- ICloneable.Clone()
所有使用的具体类型的正确 CloneCore()
实施
all use the correct CloneCore()
implementation for the concrete type.
这篇关于为什么要使用显式接口实现调用一个保护方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!