Java覆盖两个接口,碰到方法名 [英] Java overriding two interfaces, clash of method names

查看:137
本文介绍了Java覆盖两个接口,碰到方法名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个类中实现 Map< V,K> Collection< V> 但是 remove(Object)方法出现在两个接口中,因此eclipse会显示一些错误。返回类型不同,一个返回 boolean ,另一个返回 V ,但这似乎没有关系。 p>

有没有方法告诉java / eclipse哪个方法实际上被重写?



编辑:我有一个接口,所有值必须实现,它提供一个getKey()方法的值,使得可以为地图写一个add函数。但是似乎没有办法让这个类看作一个地图和一个集合在同一时间?

解决方案

否,没有直接的方式。



实际上,动态绑定考虑了签名,不包括返回类型,所以Java编译器不能接受两个方法为同一个类有相同签名但不同的返回类型。如果两个方法有相同的名称和相同的参数,他们必须也有相同的返回类型,不幸的是你。



唯一的方法是将行为分成两个不同的类,组成他们。也许可以使用 Collection< V> asCollection()或类似的东西。


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?

解决方案

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.

这篇关于Java覆盖两个接口,碰到方法名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆