“新”表达中的卷曲括号? (例如“new MyClass(){...}”)

时间:2022-07-17 22:31:21

What do the curly braces do there ?

花括号做什么?

handler1 = new Handler() {

        public void handleMessage() {

       }
};

object = new Class_Name() {}; ? This syntax exists only on Android or Java also? And what is it called in Java? Thank for your helps.

object = new Class_Name(){}; ?这种语法只存在于Android或Java上吗?它在Java中被称为什么?谢谢你的帮助。

2 个解决方案

#1


25  

This is the syntax for creating an instance of anonymous class that extends Handler. This is part of Java.

这是用于创建扩展Handler的匿名类实例的语法。这是Java的一部分。

#2


2  

This is happned when you create the instance reference of the Interface. For example I want to create the instance of the interface Runnable with the class, then I can create it by creating an anonymous class for the same and override the run() method of the interface. You can understand well by looking at the another example other then you stated below.

创建接口的实例引用时,这很愉快。例如,我想用类创建Runnable接口的实例,然后我可以通过为它创建一个匿名类来创建它,并覆盖接口的run()方法。你可以通过查看另一个你在下面说明的另一个例子来理解。

Runnable runnable = new Runnable() {

    public void run() {
        // TODO Auto-generated method stub

    }
};

#1


25  

This is the syntax for creating an instance of anonymous class that extends Handler. This is part of Java.

这是用于创建扩展Handler的匿名类实例的语法。这是Java的一部分。

#2


2  

This is happned when you create the instance reference of the Interface. For example I want to create the instance of the interface Runnable with the class, then I can create it by creating an anonymous class for the same and override the run() method of the interface. You can understand well by looking at the another example other then you stated below.

创建接口的实例引用时,这很愉快。例如,我想用类创建Runnable接口的实例,然后我可以通过为它创建一个匿名类来创建它,并覆盖接口的run()方法。你可以通过查看另一个你在下面说明的另一个例子来理解。

Runnable runnable = new Runnable() {

    public void run() {
        // TODO Auto-generated method stub

    }
};