这个花括号约定的名称是什么?

发布于 2024-10-07 18:07:00 字数 601 浏览 1 评论 0原文

我对看到有多少开发人员在类名或方法下方使用大括号编写方法和类感到有点困惑。他们遵循什么约定?

的声明是:

// this declaration follows sun's conventions
class Sample extends Object {

    Sample(int i, int j) {
        ....
    }
}

Sun 明确指出正确 更多的是,我看到这样的声明(即使在书中):

// this declaration follows a convention I cant identify
class Sample extends Object 
{

    Sample(int i, int j) 
    {
        ....
    }
}

我知道惯例只是惯例,只要你遵循它,一切都很好。我只是在寻找后一个声明遵循什么约定的答案?

I'm a bit puzzled by the number of developers I see writing methods and classes with curly braces below the class name or the method. What convention are they following?

Sun clearly states that the correct declaration would be:

// this declaration follows sun's conventions
class Sample extends Object {

    Sample(int i, int j) {
        ....
    }
}

Yet more and more I see this declared as (even in books):

// this declaration follows a convention I cant identify
class Sample extends Object 
{

    Sample(int i, int j) 
    {
        ....
    }
}

And I know that a convention is just a convention and, as long as you follow one, all is good. I'm just looking for the answer on what convention does the latter declaration follow?

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

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

发布评论

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

评论(7

音栖息无 2024-10-14 18:07:00

这是称为 ANSI 样式的标准样式约定。除了 Java 之外,它在其他语言中也很常见。

This is a standard style convention called the ANSI style. It's common in other languages besides Java, as well.

っ〆星空下的拥抱 2024-10-14 18:07:00

正如你所说,你已经见过很多次了——这是否使它本身成为一个约定,无论该约定是否有单一的来源或名称?它很容易描述,我认为不需要特定的归属。维基百科的“缩进样式”页面将其称为Allman 风格 或 ANSI 风格,但坦率地说,我已经使用它很多年了,从未听说过这个名字,而且我也不知道认为知道名称会改变我的编码方式:)

与公共类型和方法的命名等约定不同,大括号位置对使用代码的编译版本的开发人员没有影响......所以没关系(IMO )对于不同的项目和公司对此有不同的看法...同样,是否使用空格或制表符,用于缩进的空格数量等。我怀疑这就是为什么您看到支撑(可能还有私有变量)有更多变化的原因)而不是惯例的其他方面。

As you say, you've seen it quite a lot - doesn't that make it a convention in its own right, whether or not there's a single source or name for that convention? It's so easy to describe, I don't think it needs a particular attribution. Wikipedia's "indent style" page calls it the Allman Style or ANSI style, but frankly I've been using it for years without ever hearing that name, and I don't think knowing the name is going to change anything about the way I code :)

Unlike conventions such as the naming of public types and methods, brace location has no impact on developers using the compiled version of your code... so it's okay (IMO) for different projects and companies to have different takes on this... likewise whether to use spaces or tabs, the number of spaces to use for indentation etc. I suspect that's why you've seen more variation on bracing (and probably private variables) than on other aspects of convention.

或十年 2024-10-14 18:07:00

请参阅:http://en.wikipedia.org/wiki/Indent_style

Syrion 已将其标识为ANSI 风格,Sun 遵循 K&R 风格的大括号放置。

切线:现在我知道这种被诅咒的风格被称为Whitesmiths风格

See: http://en.wikipedia.org/wiki/Indent_style

Syrion has identified it as the ANSI style, Sun follows the K&R style of brace placement.

Tangent: and now I know that accursed style is called Whitesmiths style

空宴 2024-10-14 18:07:00

我更喜欢第二种约定,因为右括号位于左括号的正下方,因此更容易找到。

I prefer the second convention because the closing bracket will be directly below the opening one, so it's easier to find.

吾家有女初长成 2024-10-14 18:07:00

后者看起来像 Allman 缩进样式 ,它在 C++ 中非常流行,但是我还没有看到它在 Java 中经常使用。

The latter looks like the Allman indent style which is quite popular in C++ but I haven't seen it used often in Java.

べ繥欢鉨o。 2024-10-14 18:07:00

第二种约定也是 Visual Studio 对于 C# 的默认缩进样式(因此相当常见)

The second convention is also the default indentation style of Visual Studio (and therefore rather common) for C#

眼睛会笑 2024-10-14 18:07:00

第一次会议节省了宝贵的空间。括号匹配无论如何都是由编辑完成的。

The first convention saves precious space. The bracket matching is done by the editor anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文