Can I write validation logic in setter methods?

2020-02-05 07:29发布

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条回答
别忘想泡老子
2楼-- · 2020-02-05 08:12

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...

查看更多
登录 后发表回答