有没有理由在接口中隐藏继承的成员?

Is there ever a reason to hide inherited members in an interface?(有没有理由在接口中隐藏继承的成员?)
本文介绍了有没有理由在接口中隐藏继承的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解从另一个类继承的类可能会通过使用 new 关键字来隐藏属性.然而,这隐藏了该属性的特定实现,所以我可以看到它是如何使用的.

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.

在实现其他接口的接口中隐藏成员是否有任何实际理由?例如考虑下面的例子.IChildInterface 实现 IParentInterface,并隐藏 PropertyA.

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?

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

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();
}

IEnumerable<T> 的设计者希望与 IEnumerable 向后兼容,但也希望确保每次使用 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

这篇关于有没有理由在接口中隐藏继承的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)