我可以重命名 Java 中已实现的方法吗?
我有一个正在实现接口的类,其中一个方法称为 onClick。有没有办法实现界面想要的 onClick 但将其命名为其他名称?像这样的东西(我正在编造这个):
public void AnyMethodNameIWant() implements Interface1.onClick
我问的三个原因是:
- 查看方法签名并知道它是 来自接口
- 为了避免像 onClick 这样的“通用”名称,接口可能需要我具有
- 区分许多接口中的相同方法名称
抱歉,如果这从根本上来说是一个“糟糕”的问题,因为我是 Java 新手。
I have a class which is implementing an interface, and one of the methods is called onClick. Is there a way to implement the onClick that the interface wants but name it something else? Something like (and I'm making this up):
public void AnyMethodNameIWant() implements Interface1.onClick
Three reasons I'm asking are:
- It would be nice to look at an method signature and know that it's
coming from an interface - To avoid 'generic' names like onClick that an interface may require me to have
- To distinguish between the same method names in many interfaces
Apologies if this is a fundamentally 'bad' question as I am new to Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,你不能。接口必须通过Java中的同名方法来实现。
不过,您可以在接口实现中使用 @Override 注释(从 Java 6 开始),这有助于澄清这是一个不能随意重命名的方法。
针对第二个问题的一个选择可能是创建一个实现类只是,以便将调用转发到更具体的方法。您可能希望将其作为嵌套类甚至匿名类来执行。不过,我不确定我通常会这样做。
编辑:看到第三个问题 - 不,如果你有两个在 Java 中具有相同方法签名的接口,你只能提供一种实现:(哦,如果你有两个具有相同签名但返回类型不同的接口,更糟糕的是,您总是可以编写一个
Interface1 getInterface1()
方法,该方法返回一个匿名内部类的实例,将Interface1
方法代理到“main”类上。No, you can't. Interfaces have to be implemented by a method of the same name in Java.
You can use the
@Override
annotation with interface implementations (as of Java 6) though, which helps to clarify that this is a method which can't just be renamed arbitrarily.One option for your second issue might be to create an implementation class just for the purpose of forwarding on the call to a more specific method. You might want to do this as a nested or even anonymous class. I'm not sure I'd usually do this though.
EDIT: Having seen the third question - no, if you have two interfaces with the same method signature in Java, you can only provide one implementation :( Oh, and if you've got two interfaces with the same signature but different return types, it's even worse. You could always write a method of
Interface1 getInterface1()
which returns an instance of an anonymous inner class proxying theInterface1
methods onto the "main" class.没有。
您唯一可以做的就是添加一个实现该接口并调用您的方法的影子方法。
Nope.
The only thing you could do is add a shadow method that implements the interface and calls your method.
这两点
通常可以通过使用 适配器模式。
基本上,您创建一个中间步骤,将对任何接口方法的所有调用转发到真正的实现。
命名和签名可能有所不同。如果您愿意的话,您还可以为不同的接口提供不同的适配器(或者如果两个接口都有具有不同行为的竞争方法)。
如何分发适配器实例有很多可能性 - 在某些情况下每次创建一个新的适配器实例可能并不明智。
当然,这种模式并不是为了好玩或为了最少和干净的代码而实现的。但它可以解决实际问题。
Those two points
are usually solved by using the Adapter Pattern.
Basically you create an intermediate step which forwards all calls to any interface method to the real implementation.
Naming and signatures can vary. You can also offer different adapters for different interfaces if you want (or must if both interfaces have competing method with different behaviour).
There are quite some possibilities how to hand out the adapter instance - creating a new one every time might not be wise in certain circumstances.
Of course this pattern is nothing you implement just for fun or just for minimal and clean code. But it can solve real problems.
您无法重命名该方法,但可以定义这两个方法(
onClick
和anyMethodNameIWant
),并让onClick
只需调用您的其他方法。You can't rename the method, but you could define both methods (
onClick
andanyMethodNameIWant
) and haveonClick
just simply call your other method.当然,您可以使用具有不同名称的其他方法来指向相同的实现,以获得您想要的命名。
You of course can have additional methods with different names that point to the same implementation to get the naming you desire.