在 Java 中调用的这种类型的方法覆盖是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1542410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:01:17  来源:igfitidea点击:

What is this type of method overriding called in Java?

javaoverridinganonymous-inner-class

提问by

I'm relatively new to Java and I'm using a new API. I came across this method override and I'm not sure what this is called:

我对 Java 比较陌生,我正在使用新的 API。我遇到了这个方法覆盖,我不确定这叫什么:

public void exampleMethod() {
    Button loginButton = new Button("login"){
       public void onSubmit(){
          //submit code here
       }
    };
}

From what I understand, this is overriding the onSubmit method of the Button class. I've never come across this type of overriding before. Is there a specific name for it? I want to read up more about it but I can't find it. All my searches so far result to regular method overriding by creating a new class, which is what I'm already familiar with.

据我了解,这是覆盖 Button 类的 onSubmit 方法。我以前从未遇到过这种类型的覆盖。有它的具体名称吗?我想阅读更多关于它的信息,但我找不到它。到目前为止,我所有的搜索结果都是通过创建一个新类来覆盖常规方法,这是我已经熟悉的。

I'd appreciate if someone could point me in the right direction.

如果有人能指出我正确的方向,我将不胜感激。

Thanks.

谢谢。

采纳答案by Dave Webb

That's an anonymous inner class.

那是一个匿名内部类

In the example above instead of creating a private classthat extends Buttonwe create an subclass of Button and provide the implementation of the overridden method in line with the rest of the code.

在上面的示例中,我们创建了一个 Button 的子类,而不是创建一个private class扩展Button的 Button,并提供与其余代码一致的重写方法的实现。

As this new class is created on the fly it has no name, hence anonymous. As it's defined inside another class it's an anonymous innerclass.

由于这个新类是动态创建的,它没有名字,因此是匿名的。因为它是在另一个类中定义的,所以它是一个匿名内部类。

It can be a very handy shortcut, especially for Listenerclasses, but it can make your code hard to follow if you get carried away and the in line method definitions get too long.

它可以是一个非常方便的快捷方式,尤其是对于Listener类,但是如果您被带走并且内联方法定义太长,它会使您的代码难以理解。

回答by Jon Skeet

That's an anonymous inner class. Basically it creates a new class which derives from the specified one (Buttonin this case, although you can use the same technique to implement interfaces) and overrides appropriate methods. It can contain other methods as well, but they'd only be available within that class.

那是一个匿名内部类。基本上,它创建一个派生自指定类的新类(Button在这种情况下,尽管您可以使用相同的技术来实现接口)并覆盖适当的方法。它也可以包含其他方法,但它们只能在该类中可用。

The class has access to final local variables within the same method, and if you're writing an instance method it has an implicit reference to thisas well (so you can call other methods in your "main" class).

该类可以访问同一方法中的最终局部变量,如果您正在编写一个实例方法,它也有一个隐式引用this(因此您可以在“主”类中调用其他方法)。

回答by Jesper

That is an anonymous inner class.

那是一个匿名内部类。

More info: Anonymous classes

更多信息:匿名类