排序Java方法的最常用方法是什么? [英] What is the most common way of ordering Java methods?

查看:78
本文介绍了排序Java方法的最常用方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

订购Java方法可能是有史以来最不重要的代码样式问题,但是我正在尝试开发一种与大多数人尽可能相似的代码样式.到目前为止,这是最流行的方法,它首先声明所有字段,然后声明所有方法,所以我只问方法.

Ordering Java methods is probably the least important code style issue ever, but I'm trying to develop a code style as similar to what most people do as possible. It's by far the most popular approach to first declare all fields and then all methods, so I'll only ask about methods.

现在,我想知道如何订购Java方法.我可以想到两种明智的基本方法:

Now I'm wondering how to order Java methods. I can think of two sensible basic approaches:

  1. 按可见性排序方法(即首先公开,然后受保护,然后是私有或其他方式)
  2. 按相关性对方法进行排序(即,如果方法a()调用方法b(),则将它们尽可能紧密地放在一起)

据我所知,第二种方法到目前为止似乎更受欢迎.尽管如此,在两种情况下都存在方向问题,而且大多数人使用的还不清楚.在第二种方法中,可以将a()放在b()之上或之下.我认为将b()放在a()之上(并最终将main()放在类文件的底部)在C语言中更为常见,但不确定C ++.另一种方法是从上到下更好地阅读IMO. Java中最常见的方法是什么?关于静态字段/方法有什么特殊规则吗?

As far as I see it, the second approach seems to be more popular by far. Nonetheless, in both cases there is the question of the direction, and it is not as clear what most people use. In the second approach, you can either place a() above or below b(). I think placing b() above a() (and eventually main() at the bottom of the class file) is more common in C, not sure about C++. The other way around is IMO better for reading, from top to bottom. What's the most common approach in Java? Any special rules about static fields/methods?

推荐答案

我通常会选择选项2-IDE中的类概述显示公共和私有方法,而自动完成仅显示可见的类,因此确实没有那么重要了,就像@Visage提到的那样.

I would generally prefer option 2 - with a class overview in the IDE showing public and private methods and auto-complete only showing the visible ones for a class, it does matter less though, as @Visage mentions.

这篇关于排序Java方法的最常用方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆