在内部使用属性有什么好处? [英] What are the benefits of using properties internally?

查看:22
本文介绍了在内部使用属性有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从类外部访问成员时,封装显然是有用和必不可少的,但是在内部引用类变量时,是调用它们的私有成员还是使用它们的getter更好?如果您的 getter 只是返回变量,是否有任何性能差异?

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 private members, or use their getters? If your getter simply returns the variable, is there any performance difference?

推荐答案

不应该有显着的性能差异,您坚持使用属性的原因是因为这就是封装的全部意义所在.它使这些私有成员的所有访问保持一致和受控.因此,如果您想更改属性 getter/setter,您不必考虑我是否需要在我决定直接访问私有成员的地方的其他地方复制相同的功能?"

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?"

这篇关于在内部使用属性有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆