Can I write validation logic in setter methods?

后端 未结 7 1066
自闭症患者
自闭症患者 2020-12-09 17:49

Are setter methods only used to set the value of attributes as it is passed as argument? Can we write some validation logic before assigning the value to the attributes?

7条回答
  •  隐瞒了意图╮
    2020-12-09 18:04

    There is absolutely nothing stopping you from doing any kind of other operation inside a setter of a property. You could do anything from validation to setting the value of some other property etc. etc. Thats not to say you should however. Use your good judgement and common sense to decide what to put in there. If the setter is bulked out with innumerable lines of code then you should question your program structure...

提交回复
热议问题