gpt4 book ai didi

Java:类中方法的顺序

转载 作者:行者123 更新时间:2023-12-01 06:31:57 25 4
gpt4 key购买 nike

我一直在四处寻找是否有这方面的约定,但我没有看到。我想看看整个社区做了什么。

当你有 Java 中的任何类时,你是否以任何特定的方式组织方法?有这方面的约定吗?

例如,也许首先列出公共(public)静态成员,然后是公共(public)成员,然后是私有(private)成员?也许按字母顺序排列?也许按照方法调用的顺序,即首先初始化方法,然后是辅助方法/辅助方法,最后是清理方法?

你做什么,为什么?有建议的方法吗?

最佳答案

Google's Java Style Guide中的相关部分说:

The ordering of the members of a class can have a great effect on learnability, but there is no single correct recipe for how to do it. Different classes may order their members differently.

What is important is that each class order its members in some logical order, which its maintainer could explain if asked. For example, new methods are not just habitually added to the end of the class, as that would yield "chronological by date added" ordering, which is not a logical ordering.

显然,这只是一种风格指南,不应被视为唯一选择;其他存在并且可能更适合您/您团队的情况。

关于Java:类中方法的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37413401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com