Java重写两个接口,方法名称冲突
我正在一个类中实现 Map
和 Collection
接口,但出现 remove(Object)
方法在这两个界面中,因此 eclipse 都会向我显示一些错误。返回类型不同,一个返回 boolean,另一个返回 V,但这似乎并不重要。
有没有某种方法可以告诉 java/eclipse 哪个方法实际上被覆盖?
编辑:我有一个所有值都必须实现的接口,它通过 getKey() 方法提供值,从而可以为地图编写添加函数。但似乎没有办法让这个类同时看起来像一个地图和一个集合?
I am implementing the Map<V,K>
and the Collection<V>
interface in one class, but the remove(Object)
method occurs in both interfaces, therfore eclipse shows me some errors. The return types are different, one returns boolean
and the other V
but that doesn't seem to matter.
Is there some way of telling java/eclipse which method is actually being overridden?
EDIT: I have got an interface that all values must implement, it supplies the value with a getKey() method, making it possible to write an add function for the map. But there seems to be no way to let this one class look as a map and a collection at the same time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,没有直接的方法。
实际上,动态绑定考虑了不包括返回类型的签名,因此Java编译器不能接受同一类具有相同签名但返回类型不同的两个方法。如果两个方法具有相同的名称和相同的参数,那么它们也必须具有相同的返回类型,这对您来说很不幸。
唯一的方法是将行为分成两个不同的类并组合它们。也许像
Collection这样的方法asCollection()
或类似的东西。No, there is no a direct way.
Actually dynamic binding takes into account the signature excluding the returning type so Java compiler cannot accept the two methods for the same class that have same signature but different return types. If two methods have same names and same parameters than they MUST also have same returning type, unfortunately for you.
The only way is to split the behavior in two different classes and composing them. Maybe a method like
Collection<V> asCollection()
or something like that.Map
已经有keySet()
它是键的集合。为什么您还需要该集合?如果是这样,只需执行两个方法,例如asMap
和asCollecton
,它们会返回不同的类型。The
Map
already haskeySet()
which is collection of keys. Why do you need the Collection also? If it's so, just do two methods likeasMap
andasCollecton
which do return different types.不,没有办法解决此类冲突。
您应该考虑使用组合和委托而不是至少两个接口之一的继承,或者您可以将类的功能拆分为两个类,这实际上取决于您的具体问题。
No, there isn't a way to resolve such conflicts.
You should consider to use composition and delegation instead of inheritance for at least one of the two interfaces, or you could split the functionality of your class in two classes, it really depends on your concrete problem.
您可能需要组合而不是继承。不幸的是,Java 对此没有语言级别的支持——我的意思是它可以做到,但不必要地费力。
You probably need composition instead of inheritance. Unfortunately Java has no language-level support for that - I mean it can be done but it is unnecessarily laborious.
您需要重新考虑您的设计。从根本上来说,地图与集合不同。考虑一下 Collection.add() 方法。将没有键的对象或没有值的键添加到映射中是否有意义?
你最好的选择(我认为并且取决于你的应用程序)是实现一个映射,但是当你需要一个集合时,使用它的方法之一来获取键、值或键值对的集合。
You need to rethink your design. Fundamentally, a map is different to a collection. Think about the Collection.add() method. Does it make any sense to add an object without a key or a key without a value to a map?
Your best bet (I think and depending on your application) is to implement a map but when you need a collection, use one of its methods to get the set of keys, values or key value pairs.