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

查看:30
本文介绍了是否有任何 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天全站免登陆