是否有任何Java方法排序约定? [英] Are there any Java method ordering conventions?

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

问题描述

我有一个大型课程(40个左右的方法),这是我将作为课程作业提交的软件包的一部分。目前,这些方法在实用程序公共/私人等方面相当混乱,我想以合理的方式对它们进行排序。有这样做的标准方法吗?例如。通常字段在方法之前列出,构造函数在其他方法之前列出,而getter / setter在最后;剩下的方法怎么样?

I've got a large-ish class (40 or so methods) that is part of a package I will be submitting as course-work. Currently, the methods are pretty jumbled up in terms of utility public/private etc. and I want to order them in a sensible way. Is there a standard way of doing this? E.g. normally fields are listed before methods, the constructor(s) are listed before other methods, and getters/setters last; what about the remaining methods?

推荐答案

有些约定首先列出所有公共方法,然后列出所有私有方法 - 这意味着它是如果您看到我的意思,即使没有涉及任何界面,也很容易将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.

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

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