我可以重命名 Java 中已实现的方法吗?

发布于 2024-12-07 16:20:25 字数 342 浏览 2 评论 0原文

我有一个正在实现接口的类,其中一个方法称为 onClick。有没有办法实现界面想要的 onClick 但将其命名为其他名称?像这样的东西(我正在编造这个):

public void AnyMethodNameIWant() implements Interface1.onClick

我问的三个原因是:

  1. 查看方法签名并知道它是 来自接口
  2. 为了避免像 onClick 这样的“通用”名称,接口可能需要我具有
  3. 区分许多接口中的相同方法名称

抱歉,如果这从根本上来说是一个“糟糕”的问题,因为我是 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:

  1. It would be nice to look at an method signature and know that it's
    coming from an interface
  2. To avoid 'generic' names like onClick that an interface may require me to have
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鹤舞 2024-12-14 16:20:25

不,你不能。接口必须通过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 the Interface1 methods onto the "main" class.

执手闯天涯 2024-12-14 16:20:25

没有。

您唯一可以做的就是添加一个实现该接口并调用您的方法的影子方法。

public class MyClass implements Interface1 {
    public void AnyMethodNameIWant() { ...; }
    public void onClick() { AnyMethodNameIWant(); }
}

Nope.

The only thing you could do is add a shadow method that implements the interface and calls your method.

public class MyClass implements Interface1 {
    public void AnyMethodNameIWant() { ...; }
    public void onClick() { AnyMethodNameIWant(); }
}
你的心境我的脸 2024-12-14 16:20:25

这两点

  • 为了避免界面可能要求我使用 onClick 等“通用”名称
  • 为了区分多个接口中相同的方法名

通常可以通过使用 适配器模式

interface IFoo {
    void onClick();
    void onChange();
}

class MyImpl {
    void doSomething(){
        // real code for onClick 
    }
    void doSomethingElse(){
        // real code for onChange
    }

    IFoo getFooAdapter(){
        return new IFoo() {
            @Override
            public void onClick() {
                doSomething();
            }

            @Override
            public void onChange() {
                doSomethingElse();
            }
        };
    }
}

基本上,您创建一个中间步骤,将对任何接口方法的所有调用转发到真正的实现。

命名和签名可能有所不同。如果您愿意的话,您还可以为不同的接口提供不同的适配器(或者如果两个接口都有具有不同行为的竞争方法)。

如何分发适配器实例有很多可能性 - 在某些情况下每次创建一个新的适配器实例可能并不明智。

当然,这种模式并不是为了好玩或为了最少和干净的代码而实现的。但它可以解决实际问题。

Those two points

  • To avoid 'generic' names like onClick that an interface may require me to have
  • To distinguish between the same method names in many interfaces

are usually solved by using the Adapter Pattern.

interface IFoo {
    void onClick();
    void onChange();
}

class MyImpl {
    void doSomething(){
        // real code for onClick 
    }
    void doSomethingElse(){
        // real code for onChange
    }

    IFoo getFooAdapter(){
        return new IFoo() {
            @Override
            public void onClick() {
                doSomething();
            }

            @Override
            public void onChange() {
                doSomethingElse();
            }
        };
    }
}

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.

好多鱼好多余 2024-12-14 16:20:25

您无法重命名该方法,但可以定义这两个方法(onClickanyMethodNameIWant),并让 onClick 只需调用您的其他方法。

@Override
public void onClick() {
    anyOtherMethodNameIWant();
}

You can't rename the method, but you could define both methods (onClick and anyMethodNameIWant) and have onClick just simply call your other method.

@Override
public void onClick() {
    anyOtherMethodNameIWant();
}
往日 2024-12-14 16:20:25

当然,您可以使用具有不同名称的其他方法来指向相同的实现,以获得您想要的命名。

public void interfaceMethodA(){
   // some implementation here
}

public void AnyMethodNameIWant(){
    interfaceMethodA();
}

You of course can have additional methods with different names that point to the same implementation to get the naming you desire.

public void interfaceMethodA(){
   // some implementation here
}

public void AnyMethodNameIWant(){
    interfaceMethodA();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文