Class (static) variables: First the public class variables, then the protected, and then the private.
Instance variables: First public, then protected, and then private.
Constructors
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.
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.
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
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.
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.)
发布评论
评论(8)
类(静态)变量:首先是公共类变量,然后是
受保护,然后是私有。
实例变量:首先是公共的,然后是受保护的,然后是私有的。
构造函数
方法:这些方法应该按功能分组,而不是按功能分组
而不是范围或可访问性。例如,私有类方法
可以位于两个公共实例方法之间。目标是使
更容易阅读和理解代码。
来源:https://www.oracle.com/java/technologies/javase /codeconventions-fileorganization.html
Class (static) variables: First the public class variables, then the
protected, and then the private.
Instance variables: First public, then protected, and then private.
Constructors
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
有些约定首先列出所有公共方法,然后列出所有私有方法 - 这意味着很容易将 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.
更精确的“代码约定”链接:《类和接口声明》
The more precise link to «Code Conventions»: «Class and Interface Declarations»
不确定是否有普遍接受的标准,但我自己的偏好是;
toString
、equals
和 < code>hashcode 接下来的Not sure if there is universally accepted standard but my own preferences are;
toString
,equals
andhashcode
next一个类中有 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.我的“约定”:静态在实例之前,公共在私有之前,构造函数在方法之前,但主方法在底部(如果存在)。
My "convention": static before instance, public before private, constructor before methods, but main method at the bottom (if present).
另外,如果您由于某种原因将类成员混合在一起,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
你在使用 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.)