首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这个建筑叫什么名字?

问这个建筑叫什么名字?
EN

Stack Overflow用户
提问于 2013-10-28 17:20:40
回答 1查看 116关注 0票数 2

我在读一段我没有写的代码。我无意中发现了以下声明:

代码语言:javascript
复制
    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()));
        }
    });

我理解在第二个{ ... }中调用的方法是Expectations方法。

  • 但是你怎么称呼这种代码编写呢?
  • 特别是如何称呼第二个{ ... }?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-28 17:21:35

它是一个匿名类,其中包含一个实例初始化器块。因此,要将两者分开:

代码语言:javascript
复制
// 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) {}
}

在任何构造函数的主体之前,都会同时隐式地调用实例变量初始化器,并按文本顺序调用http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.6。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19641103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档