这个建筑的名称是什么?

Ste*_*han 2 java

我正在读一个我没写过的代码.我偶然发现了以下声明:

    context.checking(new org.jmock.Expectations() {
        {
            allowing(habilitationManager).hasRole(RoleDtoEnum.ROLE_NEWS);
            will(returnValue(true));
            allowing(habilitationManager).hasRole(RoleDtoEnum.ROLE_STAT);
            will(returnValue(true));
            allowing(habilitationManager).getUser();
            will(returnValue(getUserMock()));
            oneOf(parametreService).getParametre(PPP);
            will(returnValue(getMockPPP()));
        }
    });
Run Code Online (Sandbox Code Playgroud)

据我所知,第二种方法中的方法{ ... }Expectations方法.

  • 但是你怎么称这种代码写作?
  • 特别是你怎么称呼第二个{ ... }

Jon*_*eet 7

它是一个匿名类,其中包含一个实例初始化程序块.所以将两者分开:

// This is an anonymous class
Expectations expectations = new Expectations() {
    // Class body here
};

class Foo {

    // This is an instance initializer block in a normal class
    {
        System.out.println("You'll see this via either constructor");
    }

    Foo(int x) {}

    Foo(String y) {}
}
Run Code Online (Sandbox Code Playgroud)

一个实例初始化是隐式调用,同时为实例变量初始化,在文本顺序,任何构造函数体前.