英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论