setter和getter功能。在Android系统。性能开销?开销、性能、功能、系统

2023-09-06 13:37:36 作者:思君如痴

当我学习Java标准版,getter和setter函数经常用来隐藏变量,以减少他们的直接访问。我已告诉几个来源,在Android的,你不应该使用这些功能,只有直接修改变量。原因在于有性能损失,由于开销时使用getter和setter采用的是Android。从而导致更多的内存使用和降低系统速度。

When I was learning Java standard edition, getter and setter functions were often used to hide variables and to reduce direct access to them. I have been told by several sources that in Android you should not use these functions and only modify the variables directly. The reason is that there is a performance loss due to overhead when using getters and setters is used in Android. Resulting in more memory use and slowing down the system.

有没有道理呢?如果有,为什么使用getter和setter的性能损失?

Is there any truth to this? and if there is, why the performance loss for using getter and setter?

推荐答案

由于文件中这里使用getter和setter是Android的一个坏主意。因为它说,

As of the documentation found here using a getter and setter is a bad idea in android. As it says,

这是在Android上一个坏主意。虚拟方法调用是昂贵的,   远远超过了实例字段查找。这是合理的遵循   常见的面向对象的编程实践,并有getter和   制定者的公共接口,而是一个类中,你应该永远   接入领域直接。

this is a bad idea on Android. Virtual method calls are expensive, much more so than instance field lookups. It's reasonable to follow common object-oriented programming practices and have getters and setters in the public interface, but within a class you should always access fields directly.

了解更多有关性能这里。