2

Class could have static, private, protected, public methods. Each method is made for modifying, adding, removing etc.

How do you group functions in class's code to make it clean to read? What is the best practices?

Thank you.

4

2 回答 2

1

This is how I do it for Java classes:

  1. constructors
  2. public methods from implemented interfaces
  3. public methods overridden or methods declared abstract from extended classes (not Object, see below)
  4. public methods (other than getter/setter/Object methods)
  5. getters and setters, in the order the property is declared
  6. equals, hashCode and toString
  7. private methods
  8. public static methods
于 2010-08-16T12:26:47.543 回答
0

一种约定不需要适合所有场景——通常,在我们的团队中,我们使用 C#,并且我们使用“区域”来对私有字段、静态成员、私有方法、构造函数、受保护方法和公共方法进行分组。顺序并不重要,因为 VS 可以很好地折叠所有区域,给出一个摘要视图。有时,我们也使用“覆盖”和/或“虚拟”区域。它还取决于所讨论的类的复杂性。对于少数复杂的类,您甚至会根据功能找到区域。例如,所有“解析”内容(变量、私有方法、解析中涉及的公共方法)都将集中在一个区域下。最后,目标是拥有可读(可维护)的代码,而“一致性”将是实现这一目标的工具之一——只要团队理解这一点,就应该有

于 2010-08-16T12:39:54.110 回答