是否有过一个理由隐藏​​继承成员在一个界面? [英] Is there ever a reason to hide inherited members in an interface?

查看:132
本文介绍了是否有过一个理由隐藏​​继承成员在一个界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,一个类继承自其他类可以通过使用关键字隐藏属性。然而,这是隐藏属性的具体实施,这样我就可以看到它如何被使用。

有任何实际的原因隐藏在其中实现其他的接口,接口的成员?例如,考虑下面的例子。 IChildInterface 工具 IParentInterface 和皮革 PropertyA

 接口IParentInterface
{
    字符串名称{;组; }
    INT PropertyA {获得;组; }
    INT PropertyB {获得;组; }
}

接口IChildInterface:IParentInterface
{
    INT PropertyA {获得;组; }
    INT PropertyC {获得;组; }
}
 

解决方案
  

有什么实际的道理隐藏接口成员其中实现其他的接口?

当然。该BCL本身使用此模式的事实表明该模式是实际的。例如:

  IEnumerable接口
{
    IEnumerator的的GetEnumerator();
}
IEnumerable接口< T> :IEnumerable的
{
    新的IEnumerator< T>的GetEnumerator();
}
 

的IEnumerable 1所述的设计师; T> 希望与的IEnumerable 向后兼容的,但也希望确保对的GetEnumerator 通用接口上每次使用所谓的通用版本。藏在这种情况下的适当机制

有关方法隐藏在微妙的点一些额外的讨论,请参见:

<一个href="http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx">http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx

I understand that a class which inherits from another class may hide a property by using the new keyword. This, however, is hiding a specific implementation of the property, so I can see how it could be used.

Is there any practical reason to hide members in interfaces which implement other interfaces? For example consider the example below. IChildInterface implements IParentInterface, and hides PropertyA.

interface IParentInterface
{
    string Name { get; set; }
    int PropertyA { get; set; }
    int PropertyB { get; set; }
}

interface IChildInterface : IParentInterface
{
    int PropertyA { get; set; }
    int PropertyC { get; set; }
}

解决方案

Is there any practical reason to hide members in interfaces which implement other interfaces?

Sure. The fact that the BCL itself uses this pattern is indicative that the pattern is practical. For example:

interface IEnumerable 
{ 
    IEnumerator GetEnumerator();
}
interface IEnumerable<T> : IEnumerable
{
    new IEnumerator<T> GetEnumerator();
}

The designers of IEnumerable<T> wished to be backwards-compatible with IEnumerable but also wanted to ensure that every usage of GetEnumerator on the generic interface called the generic version. Hiding is the appropriate mechanism in this case.

For some additional discussion on subtle points about method hiding, see:

http://blogs.msdn.com/b/ericlippert/archive/2008/05/21/method-hiding-apologia.aspx

这篇关于是否有过一个理由隐藏​​继承成员在一个界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆