Java 方法有排序约定吗?

发布于 2024-10-11 11:12:35 字数 1437 浏览 6 评论 0原文

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

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

发布评论

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

评论(8

Saygoodbye 2024-10-18 11:12:35
  1. 类(静态)变量:首先是公共类变量,然后是
    受保护,然后是私有。

  2. 实例变量:首先是公共的,然后是受保护的,然后是私有的。

  3. 构造函数

  4. 方法:这些方法应该按功能分组,而不是按功能分组
    而不是范围或可访问性。例如,私有类方法
    可以位于两个公共实例方法之间。目标是使
    更容易阅读和理解代码。

来源:https://www.oracle.com/java/technologies/javase /codeconventions-fileorganization.html

  1. Class (static) variables: First the public class variables, then the
    protected, and then the private.

  2. Instance variables: First public, then protected, and then private.

  3. Constructors

  4. Methods: These methods should be grouped by functionality rather
    than by scope or accessibility. For example, a private class method
    can be in between two public instance methods. The goal is to make
    reading and understanding the code easier.

Source: https://www.oracle.com/java/technologies/javase/codeconventions-fileorganization.html

掩耳倾听 2024-10-18 11:12:35

有些约定首先列出所有公共方法,然后列出所有私有方法 - 这意味着很容易将 API 与实现分开,即使不涉及接口,如果您明白我的意思的话。

另一个想法是将相关方法组合在一起——这样可以更容易地发现接缝,您可以将现有的大类分成几个更小、更有针对性的类。

Some conventions list all the public methods first, and then all the private ones - that means it's easy to separate the API from the implementation, even when there's no interface involved, if you see what I mean.

Another idea is to group related methods together - this makes it easier to spot seams where you could split your existing large class into several smaller, more targeted ones.

秉烛思 2024-10-18 11:12:35

更精确的“代码约定”链接:《类和接口声明》

The more precise link to «Code Conventions»: «Class and Interface Declarations»

十年不长 2024-10-18 11:12:35

不确定是否有普遍接受的标准,但我自己的偏好是;

  • 构造函数首先是
  • 静态方法,然后是主方法,总是在其他静态方法之前
  • ,然后是非静态方法,通常按照方法的重要性顺序,然后是它调用的任何方法。这意味着调用其他类方法的公共方法出现在顶部,而不调用其他方法的私有方法通常最终出现在底部
  • 标准方法,例如 toStringequals 和 < code>hashcode 接下来的
  • getter 和 setter 在类的底部保留了一个特殊的位置

Not sure if there is universally accepted standard but my own preferences are;

  • constructors first
  • static methods next, if there is a main method, always before other static methods
  • non static methods next, usually in order of the significance of the method followed by any methods that it calls. This means that public methods that call other class methods appear towards the top and private methods that call no other methods usually end up towards the bottom
  • standard methods like toString, equals and hashcode next
  • getters and setters have a special place reserved right at the bottom of the class
怕倦 2024-10-18 11:12:35

一个类中有 40 个方法有点多了。

将某些功能转移到其他适当命名的类中是否有意义?那么就更容易理解了。

当你的内容较少时,按照自然的阅读顺序列出它们会更容易。常见的范例是在您需要之前或之后按照您需要的顺序列出事物。

这通常意味着 main() 位于顶部或底部。

40 methods in a single class is a bit much.

Would it make sense to move some of the functionality into other - suitably named - classes? Then it is much easier to make sense of.

When you have fewer, it is much easier to list them in a natural reading order. A frequent paradigm is to list things either before or after you need them , in the order you need them.

This usually means that main() goes on top or on bottom.

对你的占有欲 2024-10-18 11:12:35

我的“约定”:静态在实例之前,公共在私有之前,构造函数在方法之前,但主方法在底部(如果存在)。

My "convention": static before instance, public before private, constructor before methods, but main method at the bottom (if present).

药祭#氼 2024-10-18 11:12:35

另外,如果您由于某种原因将类成员混合在一起,Eclipse 还提供了对类成员进行排序的可能性:

打开您的类文件,转到主菜单中的“Source”并选择“Sort Members”。

摘自此处:在 Eclipse 中对方法进行排序

Also, eclipse offers the possibility to sort class members for you, if you for some reason mixed them up:

Open your class file, the go to "Source" in the main menu and select "Sort Members".

taken from here: Sorting methods in Eclipse

赏烟花じ飞满天 2024-10-18 11:12:35

你在使用 Eclipse 吗?如果是这样,我会坚持使用默认的成员排序顺序,因为这对于阅读您的代码的人来说可能是最熟悉的(尽管这不是我最喜欢的排序顺序。)

Are you using Eclipse? If so I would stick with the default member sort order, because that is likely to be most familiar to whoever reads your code (although it is not my favourite sort order.)

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