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.
3 个解决方案
#1
28
That's an anonymous inner class.
这是一个匿名的内部阶级。
In the example above instead of creating a private class
that extends Button
we create an subclass of Button and provide the implementation of the overridden method in line with the rest of the code.
在上面的示例中,我们不是创建扩展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 inner class.
由于这个新类是动态创建的,因此没有名称,因此是匿名的。因为它是在另一个类中定义的,所以它是一个匿名的内部类。
It can be a very handy shortcut, especially for Listener
classes, but it can make your code hard to follow if you get carried away and the in line method definitions get too long.
它可以是一个非常方便的快捷方式,特别是对于Listener类,但如果你被带走并且内联方法定义太长,它可能会使你的代码难以理解。
#2
12
That's an anonymous inner class. Basically it creates a new class which derives from the specified one (Button
in 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 this
as well (so you can call other methods in your "main" class).
该类可以访问同一方法中的最终局部变量,如果您正在编写实例方法,它也会对此进行隐式引用(因此您可以调用“main”类中的其他方法)。
#1
28
That's an anonymous inner class.
这是一个匿名的内部阶级。
In the example above instead of creating a private class
that extends Button
we create an subclass of Button and provide the implementation of the overridden method in line with the rest of the code.
在上面的示例中,我们不是创建扩展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 inner class.
由于这个新类是动态创建的,因此没有名称,因此是匿名的。因为它是在另一个类中定义的,所以它是一个匿名的内部类。
It can be a very handy shortcut, especially for Listener
classes, but it can make your code hard to follow if you get carried away and the in line method definitions get too long.
它可以是一个非常方便的快捷方式,特别是对于Listener类,但如果你被带走并且内联方法定义太长,它可能会使你的代码难以理解。
#2
12
That's an anonymous inner class. Basically it creates a new class which derives from the specified one (Button
in 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 this
as well (so you can call other methods in your "main" class).
该类可以访问同一方法中的最终局部变量,如果您正在编写实例方法,它也会对此进行隐式引用(因此您可以调用“main”类中的其他方法)。