是否有过一个理由隐藏​​继承成员在一个界面?有过、界面、成员、理由

2023-09-03 12:38:56 作者:曾经的那一抹记忆.

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

有任何实际的原因隐藏在其中实现其他的接口,接口的成员?例如,考虑下面的例子。 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 通用接口上每次使用所谓的通用版本。藏在这种情况下的适当机制

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

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

 
精彩推荐
图片推荐