Does all classes need to have getters and setters? Is there some classes that may not necessarily need the usage of getters or setters?

huangapple go评论64阅读模式
英文:

Does all classes need to have getters and setters? Is there some classes that may not necessarily need the usage of getters or setters?

问题

例如,实现一个栈类或一个队列类,

我只能想到获取方法。那样可以吗?还是我真的需要为这些类找到设置方法?

英文:

E.g. Implementing a stack class or a queue class,

I can only think of getter methods. Will that be okay? Or do I really need to find setter methods for those classes?

答案1

得分: 1

获取器(Getters)和设置器(Setters)不是必需的,它们主要用于包含私有字段的公共类。如果您的堆栈/队列类需要诸如push()、pop()、drop()等方法,则您的用例不需要获取器和设置器。

示例:push() 是一个自定义的设置器方法。
pop() 是一个自定义的获取器方法。

英文:

Getters and Setters are not mandatory, they are primarily used with public classes that contain private fields.
If your stack/Queue class needs methods such as push(),pop(),drop(),etc., you don't need getters and setter for your use case.

Example: push() is a custom setter method.
pop() is a custom getter method.

huangapple
  • 本文由 发表于 2020年8月21日 11:12:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63515976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定