Allman 风格的匿名类

发布于 2024-10-08 05:40:52 字数 349 浏览 8 评论 0原文

关于如何使用匿名类同时与 Allman 缩进样式保持一致,有什么建议吗?我真的不喜欢我想出的任何东西,例如

// Pass as parameter.
foo(new Clazz( )
    {
       // Do stuff.
    });

// Assign to variable.
Clazz bar = new Clazz( )
            {
               // Do stuff.
            };

Any recommendations on how to use anonymous classes while staying consistent with Allman indent style? I don't really like anything I've come up with, e.g.

// Pass as parameter.
foo(new Clazz( )
    {
       // Do stuff.
    });

// Assign to variable.
Clazz bar = new Clazz( )
            {
               // Do stuff.
            };

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

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

发布评论

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

评论(3

ゃ人海孤独症 2024-10-15 05:40:53

奥尔曼风格实际上是对齐{大括号},而不是所有(括号)。我想如果你愿意的话,你可以自由地做这两件事,但在我看来,这似乎是问题的根源(就像这个),在可读性方面没有明显的回报。换句话说,这是一种合乎逻辑的迷恋:-)

您可以按照 http://mbreen.com/javastyle.html 上的指南进行操作
“包含带有代码块的声明的语句首先作为语句缩进。”我认为这会将你的例子改为

foo (new Clazz( )
    {
        // Do stuff.
    });

Clazz bar = (
    new Clazz( )
    {
        // Do stuff.
    });

Allman style is really about aligning the {braces}, not all the (brackets). I suppose you are free to do both if you want, but it looks like a source of problems (like this one) to me, without a clear payback in readability. In other words, a logical fetish :-)

You could follow the guide at http://mbreen.com/javastyle.html:
"A statement containing a declaration with a code block is indented first as a statement." I think that would change your examples to

foo (new Clazz( )
    {
        // Do stuff.
    });

Clazz bar = (
    new Clazz( )
    {
        // Do stuff.
    });
倥絔 2024-10-15 05:40:53

这就是我已经决定的。

Foo foo = new Foo()
{
    // Do stuff.
};

我只是不再在方法调用中定义匿名类。

This is what I've settled on.

Foo foo = new Foo()
{
    // Do stuff.
};

And I just don't define anonymous classes inside method calls anymore.

岁月静好 2024-10-15 05:40:52

我为自己的代码提出的最佳折衷方案是将匿名类缩进一个制表符级别,并将右括号放在新行上。

// Pass as parameter.
foo(new Clazz( )
    {
       // Do stuff.
    }
);

void func () {
    foo(new Clazz( )
        {
           // Do stuff.
        }
    );
}

// Assign to variable.
Clazz bar = new Clazz( )
    {
        // Do stuff.
    };

The best compromise I came up with for my own code, is indenting the anonymous class a single tabbing level, and putting the closing parentheses on a new line.

// Pass as parameter.
foo(new Clazz( )
    {
       // Do stuff.
    }
);

void func () {
    foo(new Clazz( )
        {
           // Do stuff.
        }
    );
}

// Assign to variable.
Clazz bar = new Clazz( )
    {
        // Do stuff.
    };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文