gpt4 book ai didi

java - 当场初始化一个类时,它叫什么?

转载 作者:行者123 更新时间:2023-12-02 11:11:55 24 4
gpt4 key购买 nike

像这样:

Kernel kernel = new Kernel() {};

我不知道它的名字。我想知道这叫什么,这样我就可以了解更多信息,但不幸的是我自己无法通过搜索引擎找到答案。

最佳答案

它被称为匿名类。摘自Oracle tutorial :

The anonymous class expression consists of the following:

  • The new operator

  • The name of an interface to implement or a class to extend. In this example, the anonymous class is implementing the interface HelloWorld.

  • Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. Note: When you implement an interface, there is no constructor, so you use an empty pair of parentheses, as in this example.

  • A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not.

关于java - 当场初始化一个类时,它叫什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50557666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com