What are the benefits of using properties internally?

后端 未结 5 715
猫巷女王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

    The performance difference is neglectable, in about 98% of the time.

    You should always use properties even though your getter or setter just get or set your private member. This will allow you to bring changes as your application evolves. Doing so, you'll be able to bring some restrictions or some other encapsulation within your properties without breaking your code. Otherwise, once you decide to write properties because of a reason X, you'll find yourself obliged to refactor all of your code to get or set the property instead of your private member.

提交回复
热议问题