4

在查看我从另一个开发人员那里获得的代码时,我遇到了以下代码。

public void myMethod()
{
    final MyClass data1 = new MyClass(1,2,3);
    final MyClass data2 = new MyClass(4,5,6);
    // [...]
    final MyClass dataN = new MyClass(M,O,P);

    ArrayList<MyClass> list = new ArrayList<MyClass>()
    {
        {
            add(data1);
            add(data2);
            // [...]
            add(dataN);
        }
    };
}

事实上,我想我知道这段代码的作用(填充list定义的数据),但我对结果是如何实现的感到惊讶。特别是我想知道在这种情况下花括号是什么{}意思。

我知道(认为?)这段代码很糟糕,我已经重写了它,但出于好奇,我想知道它到底做了什么。

我的猜测如下:

  • 第一对{}是匿名对象创建 - 被强制转换为ArrayList<MyClass>.
  • 第二对{}是 - 我正在考虑 - 类似于静态初始化但针对对象的东西。那可能是某种匿名构造函数吗?

有人可以在这里给我一些见解吗?(我在哪里可以在 java 文档中找到这样的“语法魔术”?)

4

5 回答 5

3

By first paranthesis, you are creating an annonymous inner, sub class of super class ArrayList. The second parenthesis is for instance initialization for the instances of your annonymous inner class. Have a look on this Doc for more details. There is a good explanation in this blog about instance initialization blocks

于 2013-02-21T12:15:35.007 回答
2
ArrayList<MyClass> list = new ArrayList<MyClass>{     //anonymous of subclass
  {     //non-static initializer block
      System.out.prinln("...");
  }
};

First of {} after new ArrayList<MyClass> which creates a new anonymous of subclass ArrayList, since ArrayList is not final class you can do it.

Second pair of {} is a non-static block or instance block inside the new sub class.


If you try Integer int1 = new Integer(10){}; this will not work because an anonymous class cannot subclass the final class Integer.

于 2013-02-21T12:21:07.133 回答
1

如果缺少括号(或分号,但这将导致完全不同的结果),此代码将不起作用。

final MyClass data1 = new MyClass(1,2,3);
final MyClass data2 = new MyClass(4,5,6);
// [...]
final MyClass dataN = new MyClass(M,O,P);

ArrayList<MyClass> list = new ArrayList<MyClass>()//here
{
    {
        add(data1);
        add(data2);
        // [...]
        add(dataN);
    }
};

第一个 { 意味着您创建了一个扩展 ArrayList 的新类。下一个 { 表示匿名块,只是对您的代码进行分组。编辑:由于这是在函数之外,因此将在创建对象时调用它。

于 2013-02-21T12:15:05.967 回答
1

This code creates an instance of an anonymous sub-class of ArrayList (the first pair of {}). This is relative question: How are Anonymous (inner) classes used in Java?

The nested block is an instance initialiser block (see http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html). This means that when the instance is created, this code will be executed before the constructor (any constructor) is executed.

   {
        add(data1);
        add(data2);
        // [...]
        add(dataN);
    }
于 2013-02-21T12:22:26.393 回答
0
 {
        {
            add(data1);
            add(data2);
            // [...]
            add(dataN);
        }
 };

This is the instance block, so after creating the object of the ArrayList (list) , this piece of code will be executed. At each line the add() method will be invoked and the data will be inserted into the ArrayList
This is equivalent to say

ArrayList<MyClass> list = new ArrayList<MyClass>();
list.add(data1);
list.add(data2);
// [...]
list.add(dataN);

As mentioned by others, doc will explain the importance of instance block.
First the static instance block is called, then the instance block and finally the constructor. The static instance block is called only once, whereas instance block and constructors are invoked whenever a new object is created.

于 2013-02-21T12:31:19.050 回答