Should I use set once variables?

后端 未结 8 754
闹比i
闹比i 2020-12-21 05:27

Does this smell?
I have a few properties you can only set once. They can be set at any time during the objects existence and can not be undone.
I implement then like

8条回答
  •  情歌与酒
    2020-12-21 05:52

    I would suggest setting them on construction, and therefore making the setters private. That seems like a more sensible way.

    If you are going to do it that way, throw some sort of Exception. At least let the developer know you didn't set their values rather than ignoring it silently.

提交回复
热议问题