为什么要显式实现接口?
那么,显式实现接口的良好用例到底是什么?
仅仅是为了让使用该类的人不必查看智能感知中的所有这些方法/属性吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
那么,显式实现接口的良好用例到底是什么?
仅仅是为了让使用该类的人不必查看智能感知中的所有这些方法/属性吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(11)
如果您实现两个使用相同方法和不同实现的接口,则必须明确实现。
If you implement two interfaces, both with the same method and different implementations, then you have to implement explicitly.
隐藏非首选成员很有用。例如,如果您同时实现
IComparable
和IComparable
,那么隐藏IComparable
重载通常会更好,以免给人这样的印象:您可以比较不同类型的对象。同样,某些接口不符合 CLS,例如IConvertible
,因此,如果您没有显式实现该接口,则需要 CLS 合规性的语言的最终用户将无法使用您的对象。 (如果 BCL 实现者没有隐藏原语的 IConvertible 成员,这将是非常灾难性的:))另一个有趣的注意事项是,通常使用这样的构造意味着显式实现接口的结构只能通过装箱到接口类型来调用它们。您可以通过使用通用约束来解决此问题::
当您向 int 传递一个 int 时,不会对它进行装箱。
It's useful to hide the non-preferred member. For instance, if you implement both
IComparable<T>
andIComparable
it is usually nicer to hide theIComparable
overload to not give people the impression that you can compare objects of different types. Similarly, some interfaces are not CLS-compliant, likeIConvertible
, so if you don't explicitly implement the interface, end users of languages that require CLS compliance cannot use your object. (Which would be very disastrous if the BCL implementers did not hide the IConvertible members of the primitives :))Another interesting note is that normally using such a construct means that struct that explicitly implement an interface can only invoke them by boxing to the interface type. You can get around this by using generic constraints::
Will not box an int when you pass one to it.
Some additional reasons to implement an interface explicitly:
backwards compatibility: In case the
ICloneable
interface changes, implementing method class members don't have to change their method signatures.清洁器代码:如果
clone
方法从iClonable中删除,则将出现编译器错误,但是,如果您隐含地实现该方法,则可以最终以未使用的“孤儿”公共方法最终出现强键:
为了用一个示例来说明Supercat的故事,这将是我首选的示例代码,实现
iClonable
明确允许clone()
直接将其直接称为> MyObject
实例成员:Some additional reasons to implement an interface explicitly:
backwards compatibility: In case the
ICloneable
interface changes, implementing method class members don't have to change their method signatures.cleaner code: there will be a compiler error if the
Clone
method is removed from ICloneable, however if you implement the method implicitly you can end up with unused 'orphaned' public methodsstrong typing:
To illustrate supercat's story with an example, this would be my preferred sample code, implementing
ICloneable
explicitly allowsClone()
to be strongly typed when you call it directly as aMyObject
instance member:另一个有用的技术是,使函数的公共实现方法返回一个比接口中指定的值更具体的值。
例如,一个对象可以实现
ICLONABLE
,但仍然具有公开可见的clone
方法返回自己的类型。同样,
iautomobileFactory
可能具有制造
方法,该方法返回automobile
> iautomobilefactory ,可能具有其制造
方法返回fordexplorer
(它源自automobile
)。代码知道它具有fordexplorerFactory
可以使用fordexplorer
- 特定的属性在由fordexplorerFactory返回的对象上仅仅知道它具有某种类型的
iautomobilefactory
只会以automobile
来处理其返回。Another useful technique is to have a function's public implementation of a method return a value which is more specific than specified in an interface.
For example, an object can implement
ICloneable
, but still have its publicly-visibleClone
method return its own type.Likewise, an
IAutomobileFactory
might have aManufacture
method which returns anAutomobile
, but aFordExplorerFactory
, which implementsIAutomobileFactory
, might have itsManufacture
method return aFordExplorer
(which derives fromAutomobile
). Code which knows that it has aFordExplorerFactory
could useFordExplorer
-specific properties on an object returned by aFordExplorerFactory
without having to typecast, while code which merely knew that it had some type ofIAutomobileFactory
would simply deal with its return as anAutomobile
.当您有两个具有相同成员名称和签名的接口时,它也很有用,但是希望根据其使用方式更改其行为。 (我不建议这样写这样的代码):
It's also useful when you have two interfaces with the same member name and signature, but want to change the behavior of it depending how it's used. (I don't recommend writing code like this):
它可以使公共接口清洁器明确实现接口,即您的
文件
类可能实现idisposable
明确提供公共方法 collect> collect()>对于消费者而言,这可能比处置(
)更有意义。f#唯一的提供明确的接口实现,因此您始终必须施放特定接口才能访问其功能,这使得非常明确(不打算)使用该接口。
It can keep the public interface cleaner to explicitly implement an interface, i.e. your
File
class might implementIDisposable
explicitly and provide a public methodClose()
which might make more sense to a consumer thanDispose(
).F# only offers explicit interface implementation so you always have to cast to the particular interface to access its functionality, which makes for a very explicit (no pun intended) use of the interface.
如果您具有内部接口,并且不想公开实现课程成员,则可以明确实施它们。隐性实现必须公开。
If you have an internal interface and you don't want to implement the members on your class publicly, you would implement them explicitly. Implicit implementations are required to be public.
显式实现的另一个原因是为了可维护性。
当一个类变得“忙碌”时——是的,这种情况发生了,我们并不都有能力重构其他团队成员的代码——然后有一个显式的实现可以清楚地表明那里有一个方法来满足接口契约。
所以它提高了代码的“可读性”。
Another reason for explicit implementation is for maintainability.
When a class gets "busy"--yes it happens, we don't all have the luxury of refactoring other team members' code--then having an explicit implementation makes it clear that a method is in there to satisfy an interface contract.
So it improves the code's "readability".
System.Collections.Immutable 给出了一个不同的示例,其中作者选择使用该技术为集合类型保留熟悉的 API,同时删除接口中对其没有任何意义的部分。新类型。
具体来说,
ImmutableList
实现IList
,因此ICollection
(为了允许ImmutableList
更轻松地与旧代码一起使用),但void ICollection.Add(T item)
对于ImmutableList
没有意义:因为向不可变列表添加元素不得更改现有列表,ImmutableList
也源自IImmutableList
其IImmutableList Add(T item)
可用于不可变列表。因此,在
Add
的情况下,ImmutableList
中的实现最终如下所示:A different example is given by
System.Collections.Immutable
, in which the authors opted to use the technique to preserve a familiar API for collection types while scraping away the parts of the interface that carry no meaning for their new types.Concretely,
ImmutableList<T>
implementsIList<T>
and thusICollection<T>
(in order to allowImmutableList<T>
to be used more easily with legacy code), yetvoid ICollection<T>.Add(T item)
makes no sense for anImmutableList<T>
: since adding an element to an immutable list must not change the existing list,ImmutableList<T>
also derives fromIImmutableList<T>
whoseIImmutableList<T> Add(T item)
can be used for immutable lists.Thus in the case of
Add
, the implementations inImmutableList<T>
end up looking as follows:在显式定义的接口的情况下,所有方法自动都是私有的,您不能将访问修饰符授予它们公共。认为:
In case of explicitly defined interfaces, all methods are automatically private, you can't give access modifier public to them. Suppose:
这就是我们创建显式接口的方法:
如果我们有 2 个接口,并且两个接口都有相同的方法,并且单个类继承这 2 个接口,那么当我们调用一个接口方法时,编译器会混淆要调用哪个方法,所以我们可以使用显式接口来解决这个问题。
这是我在下面给出的一个例子。
This is how we can create Explicit Interface:
If we have 2 interface and both the interface have the same method and a single class inherit these 2 interfaces so when we call one interface method the compiler got confused which method to be called, so we can manage this problem using Explicit Interface.
Here is one example i have given below.