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.
我在一个类中实现了Map
Is there some way of telling java/eclipse which method is actually being overridden?
有没有办法告诉java / eclipse哪个方法实际被覆盖?
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?
编辑:我有一个所有值必须实现的接口,它使用getKey()方法提供值,从而可以为地图编写一个add函数。但是似乎没有办法让这个类同时看起来像一个地图和一个集合?
5 个解决方案
#1
8
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.
实际上,动态绑定会考虑不包括返回类型的签名,因此Java编译器不能为具有相同签名但返回类型不同的同一类接受这两种方法。如果两个方法具有相同的名称和相同的参数,那么它们也必须具有相同的返回类型,不幸的是你。
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.
唯一的方法是将行为分成两个不同的类并组成它们。也许像Collection
#2
1
The Map
already has keySet()
which is collection of keys. Why do you need the Collection also? If it's so, just do two methods like asMap
and asCollecton
which do return different types.
Map已经有keySet(),它是键的集合。你为什么还需要收藏品呢?如果是这样,只需要做两个方法,比如asMap和asCollecton,它们会返回不同的类型。
#3
1
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.
你应该考虑在两个接口中至少有一个使用组合和委托而不是继承,或者你可以在两个类中拆分你的类的功能,这实际上取决于你的具体问题。
#4
0
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.
您可能需要组合而不是继承。不幸的是,Java没有语言级别的支持 - 我的意思是它可以完成,但它是不必要的费力。
#5
0
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?
你需要重新考虑你的设计。从根本上说,地图与集合不同。想想Collection.add()方法。没有键或没有值的键添加对象是否有意义?
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.
您最好的选择(我认为并且取决于您的应用程序)是实现地图,但是当您需要集合时,请使用其中一种方法来获取密钥,值或键值对的集合。
#1
8
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.
实际上,动态绑定会考虑不包括返回类型的签名,因此Java编译器不能为具有相同签名但返回类型不同的同一类接受这两种方法。如果两个方法具有相同的名称和相同的参数,那么它们也必须具有相同的返回类型,不幸的是你。
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.
唯一的方法是将行为分成两个不同的类并组成它们。也许像Collection
#2
1
The Map
already has keySet()
which is collection of keys. Why do you need the Collection also? If it's so, just do two methods like asMap
and asCollecton
which do return different types.
Map已经有keySet(),它是键的集合。你为什么还需要收藏品呢?如果是这样,只需要做两个方法,比如asMap和asCollecton,它们会返回不同的类型。
#3
1
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.
你应该考虑在两个接口中至少有一个使用组合和委托而不是继承,或者你可以在两个类中拆分你的类的功能,这实际上取决于你的具体问题。
#4
0
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.
您可能需要组合而不是继承。不幸的是,Java没有语言级别的支持 - 我的意思是它可以完成,但它是不必要的费力。
#5
0
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?
你需要重新考虑你的设计。从根本上说,地图与集合不同。想想Collection.add()方法。没有键或没有值的键添加对象是否有意义?
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.
您最好的选择(我认为并且取决于您的应用程序)是实现地图,但是当您需要集合时,请使用其中一种方法来获取密钥,值或键值对的集合。