Java Conventions: use getters/setters WITHIN the class?

前端 未结 12 1841
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 02:32

My professor really emphasizes protecting against privacy leaks by always using accessors and mutators to access private instance variables; however, do I have to use the ge

12条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 02:37

    If your class (or the accessor methods in question) is not final, then you should definitely use the accessor methods.

    If another class extends yours and overrides those accessors, your class should use the overridden accessors. If this would break your superclass, then your superclass is designed incorrectly; prevent those accessors from being overridden with final, or change the design of your class.

提交回复
热议问题