setter和getter功能。在Android系统。性能开销? [英] Setter and Getter functions. In Android. Performance overhead?

查看:192
本文介绍了setter和getter功能。在Android系统。性能开销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我学习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.

了解更多有关性能这里

这篇关于setter和getter功能。在Android系统。性能开销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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