Java 声明(排序)

发布于 2024-08-27 15:43:51 字数 155 浏览 6 评论 0原文

在 Java 中,根据声明的数据成员和方法在类文件中列出的顺序来组织类的最常见方式是什么,请记住以下内容以及您可以为每个类想到的任何其他内容

  • :可见性
  • 是否是构造函数、方法或成员
  • (如果是方法)是否重载或覆盖其他方法?

In Java, what's generally the most accepted way to organize a class in terms of the order in which declared data members and methods should be listed in the class file, keeping in mind the following and anything else you can think of for each one:

  • its visibility
  • whether it's a constructor, method, or member
  • if it's a method, does it overload, or override other method(s)?

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

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

发布评论

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

评论(4

一曲琵琶半遮面シ 2024-09-03 15:43:51

我发现 JavaTM 编程语言的代码约定 非常合适。第三章回答了这个问题。

大多数情况下,它忽略可见性或它是否是重载函数等。

它的顺序很简单:

  • 类(静态)变量
  • 实例变量
  • 构造函数
  • 方法

可以采取更严格的方法,但我没有看到有利于它的强有力的论据。

I've found that the conventions in Code Conventions for the JavaTM Programming Language to be quite suitable. Chapter 3 answers this question.

Mostly it ignores visibility or whether it is an overloaded function etc.

It is simply in order:

  • Class (static) variables
  • Instance variables
  • Constructor
  • Methods

A more rigorous approach could be taken, but I do not see a strong argument in its favor.

方圜几里 2024-09-03 15:43:51

有建议的最佳实践,克里斯提到的那些几乎是“标准”,内部类位于该列表的底部。
也就是说,如果您使用 Intellij IDEA (有一个免费的社区版本)是一个名为 Rearranger 的插件,它可以让您指定所有这些并强制执行它,更重要的是重构现有代码,使其成为您认为应该的样子。

There are suggested best practices and the ones Kris mentions are pretty much "the" standard, with Inner Classes at the bottom of that list.
That said, if you are using Intellij IDEA (there is a free Community version) there is a plug-in called Rearranger, that lets you specify all this and enforce it and more importantly refactor existing code to be what you think it should be.

魂牵梦绕锁你心扉 2024-09-03 15:43:51

如果需要,您可以遵循 Google 项目 GWT 使用的建议。它基于 Sun 的标准 Java 代码约定。它添加了一些严格的排序规则,还涉及可见性修饰符等:

http://code .google.com/webtoolkit/makinggwtbetter.html#codestyle

You could follow the recommendation used by the Google project GWT, if you want. It's based on the standard Java code conventions from Sun. It adds some strict rules on ordering, also concerning visibility modifiers etc.:

http://code.google.com/webtoolkit/makinggwtbetter.html#codestyle

二智少女 2024-09-03 15:43:51

Eclipse(我确信还有其他 IDE)将自动对声明进行排序(源 -> 排序成员),并提供类型和可见性选项。重载和重写不可用,至少在 Eclipse 中是这样。因此,我想说建立一个约定,其中包括类型和可见性,但不包括过载/覆盖状态,然后配置团队的 IDE 来实施该约定。团队更容易遵守自动化和自动化标准。

Eclipse (and, I'm sure, other IDEs) will automatically order declarations (Source -> Sort Members), with options for types and visibility. Overloading and overriding aren't available, at least in Eclipse. So I would say establish a convention that includes type and maybe visibility, but not overload/override status, then configure your team's IDE to impose that convention. Automatable - and automated - standards are much easier for the team to adhere to.

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