フリー問題

Java SE 11 Developer のフリー問題 16 / 20 問目

問題文

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?

選択肢

  1. 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
  2. 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
  3. 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
  4. 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

解答・解説を確認するには

正解と解説の確認、回答の記録には無料登録が必要です。登録すると演習モードでフリー問題に回答し、正誤と解説をその場で確認できます。