且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Getter-Setter和私有变量

更新时间:2022-12-24 20:02:42

是的,确实如此。我在清洁代码一书的getter和setter中得出以下结论;如果你真的接受它就可以使用它。

Yes, It does. I have the following conclusion in getters and setters from the Clean Code book; you can use it if you really accept it.


  1. 非常邪恶:公共领域。

  2. 有点邪恶:吸气剂和二传手不需要它们。

  3. 好:只有在真正需要的地方才有吸气剂和制定者 - 使
    类型暴露更大的行为恰好使用其状态
    而不仅仅是将类型视为由其他类型操纵的
    状态的存储库。

  1. Very evil: public fields.
  2. Somewhat evil: Getters and setters where they're not required.
  3. Good: Getters and setters only where they're really required - make the type expose "larger" behaviour which happens to use its state, rather than just treating the type as a repository of state to be manipulated by other types.