对象创建过程中的Java覆盖 [英] java override during object creation
问题描述
在下面的Java代码中,创建了一个JButton,但与此同时,其一个方法被覆盖. 问题:在创建对象时是否存在以这种方式覆盖的名称?
in the following java code a JButton is created but at the same time one of its methods gets overridden. Qestion: is there a name for overriding in this way while creating the object?
代码:
JButton myButton;
myButton = new JButton ("ok"){
@Override
public void setText(String text) {
super.setText(text +", delete");
}
jbutton的标签现在为确定,删除"
the jbutton's label is now "ok, delete"
推荐答案
这是一个匿名类.来自简而言之Java
That's an anonymous class. From Java in a Nutshell
匿名类是本地类 没有名字匿名课程是 一次定义和实例化 使用新的简洁表达 操作员.虽然是本地班 定义是一个语句块 Java代码,一个匿名类 定义是一个表达式,其中 意味着它可以作为一部分包含在内 较大的表达式,例如 方法调用.当本地课程是 仅使用一次,请考虑使用 匿名类语法,该语法将 类的定义和使用 完全一样的地方.
An anonymous class is a local class without a name. An anonymous class is defined and instantiated in a single succinct expression using the new operator. While a local class definition is a statement in a block of Java code, an anonymous class definition is an expression, which means that it can be included as part of a larger expression, such as a method call. When a local class is used only once, consider using anonymous class syntax, which places the definition and use of the class in exactly the same place.
这是在不通过class
表达式显式定义新类的情况下提供基类特化的一种常用方法.
It's a common means of providing a specialisation of a base class without explicitly defining a new class via the class
expression.
这篇关于对象创建过程中的Java覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!