What are the benefits of using properties internally?

后端 未结 5 720
猫巷女王i
猫巷女王i 2020-12-10 19:19

Encapsulation is obviously helpful and essential when accessing members from outside the class, but when referring to class variables internally, is it better to call their

5条回答
  •  佛祖请我去吃肉
    2020-12-10 19:22

    There shouldn't be a significant performance difference, and the reason you stick to using the properties is because that's the whole point of encapsulation. It keeps all accesses of those private members consistent and controlled. So if you want to change the property getter/setter you don't have to think "do I need to duplicate the same functionality elsewhere in the places where I decided to access the private member directly?"

提交回复
热议问题