問題文
A record is declared with two components and needs to reject negative values for the second component. Which approach follows the intended design of record classes?
選択肢
- A compact constructor that validates the parameter and then assigns both fields, which its body has to spell out.
- Declare a static factory method that validates the value and make the canonical constructor private so callers cannot bypass the validation, while the compact form is left unused.
- Override the automatically generated accessor for the second component so that it throws when the stored value turns out to be negative at the time it is read.
- Declare a compact constructor that validates the parameter and leaves the assignments to the two fields for the compiler to add.