問題文
A class has to let its callers read and change one piece of its internal state, and the author is deciding between a field that the callers touch and a pair of methods. Which way does the guidance take, and on what ground?
選択肢
- A private field behind a pair of public methods is the way, because a method that sets the value lets the class record every change for a later review, and the field itself stays out of the class file of the caller
- A field that the callers touch directly is the way, because a field of that kind spares the class a layer and each caller then reads the state straight out of the object
- A private field behind a pair of public methods is the way, because a method that sets the value gives the class a place to run its validation before the new value lands, and a state that only subclasses touch may sit behind a protected pair instead
- A field that the callers touch directly is the way, because a field of that kind is read from the object itself and any validation of the state may run in its constructor instead