new Object { } 的作用是什么?

发布于 2024-12-09 09:24:01 字数 224 浏览 0 评论 0原文

很难搜索这方面的信息,因为我不知道它叫什么,所以我想这是我的第一个问题。

使用此表示法是否:

new Object() {//code here}

创建一个 Object 类型的新对象或创建一个作为 object 子类的新对象?如果它是一个子类,您可以将用于声明类的所有内容(成员变量、函数、内部类)放在大括号内吗?你甚至可以为它编写一个构造函数吗?

It was difficult to search for information on this because I don't know what it's called, so that's my first question I guess.

Does the use of this notation:

new Object() {//code here}

create a new object of type Object or create a new object that is a subclass of object? If it is a subclass, can you put everything you could use to declare a class (member variables, functions, inner classes), inside the braces? Could you even write a constructor for it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

潜移默化 2024-12-16 09:24:01

这称为匿名类。基本上,您正在就地定义一个子类。

这个调用:

Object a = new Object() {
   // stuff
};

.. 定义了一个新的 Object 子类,其中包含 //stuff (就像在普通的 A extends Object 中声明它一样。

维基百科有 一篇关于它们的好文章

它们主要用于闭包,这意味着匿名类中定义的函数可以访问封闭范围内的 final 变量

interface Function {
    void run();
}

static void example(Function f) {
    f.run();
}

void runExample() {
    final int a = 10;
    example(new Function() {
        void run() {
            // Notice that "a" was defined in the outer scope
            System.out.println(a);
        }
    });
}

This is called an anonymous class. Basically, you're defining a subclass in-place.

This call:

Object a = new Object() {
   // stuff
};

.. defines a new subclass of Object with //stuff in it (just like declaring that in a normal A extends Object.

Wikipedia has a good article about them too.

They're useful primarily for closures, which means that the functions defined in your anonymous class have access to final variables from the enclosing scope.

Example-ish:

interface Function {
    void run();
}

static void example(Function f) {
    f.run();
}

void runExample() {
    final int a = 10;
    example(new Function() {
        void run() {
            // Notice that "a" was defined in the outer scope
            System.out.println(a);
        }
    });
}
佼人 2024-12-16 09:24:01
new Object() {
    //code here
}

将创建一个扩展Object的新匿名内部类。这是一个完全有效的类,可以包含普通非匿名类所包含的任何内容。尽管在匿名类中声明新方法是非常不切实际的,因为它们的范围非常小。

new Object() {
    //code here
}

Would create a new anonymous inner class which extends Object. This is a completely valid class and may contain anything that a normal non-anonymous class contains. Though it would be quite impractical to declare new methods inside an anonymous class since they would have a very small scope.

攒一口袋星星 2024-12-16 09:24:01
new Object() {
    //code here
}

这是有效的。它将创建扩展 Object 的匿名内部类。但您将无法保存其参考。您可以使用的引用仅属于对象类。

Object ob=new Object(){
    //code
}

通过使用此引用,您只能访问 Object 类的方法。在此匿名类中声明的任何新方法都不会被访问。绝对可以使用重写方法。所以创建其他方法是没有用的,因为你永远无法使用。另外,在这个类中你不能创建构造函数,因为这是扩展Object的匿名类,并且构造函数只能用类名创建,而你不知道类名。

new Object() {
    //code here
}

It is valid. It will create Anonymous inner class which extends Object. But you will not be able to save its reference. The Reference you can use is of Object class only.

Object ob=new Object(){
    //code
}

By using this reference you can only access the methods of Object class. Any new methods declared in this anonymous class will not get accessed. Definitely overridden methods can be used. So there is no use of creating other methods as you can never use. Also in this class you can't create a constructor, as this is anonymous class which extends Object,and constructor can only be created with class name,and you don't know the class name.

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