具有相同方法名称的多个接口 [英] Multiple interfaces with same method names

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

问题描述

我有一个继承自两个不同接口的类。两个接口都声明了一个具有相同名称的方法。如何为每个接口提供不同的实现?

I have a class which inherits from two different interfaces. Both interfaces declare a method with the same name. How can I provide a different implementation for each interface ?

在C#中,答案是存在的,但它在java中不起作用:
来自具有相同方法名称的多个接口的继承

In C#, the answer is there, but it does not work in java: Inheritance from multiple interfaces with the same method name

我考虑提供一个使用类型比较的联合实现,但它有点难看。

I thought about providing a union implementation which uses type comparison but it's kind of ugly.

谢谢

编辑:关闭,我的问题与以下内容重复,谢谢你的答案!
Java - 界面实现中的方法名称冲突

EDIT : closed, my question was a duplicate of the following, thank you for the answers ! Java - Method name collision in interface implementation

推荐答案

你不能。接口描述行为,但它们不实现它。因此,如果您实现了一个方法,则无法确定从哪个接口实现它。

You can't. Interfaces describe behavior, but they don't implement it. So if you implement a method, there's no way to tell which interface you are implementing it from.

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

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