Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
例如实现堆栈类或队列类,
我只能想到getter方法。这样会好吗?还是我真的需要为这些类找到 setter 方法?
Getter 和 Setter 不是强制性的,它们主要用于包含私有字段的公共类。如果您的堆栈/队列类需要 push()、pop()、drop() 等方法,则您的用例不需要 getter 和 setter。
示例: push() 是一个自定义的 setter 方法。pop() 是一个自定义的 getter 方法。