草庐IT

android - 避免内部 getter/setter

coder 2023-11-26 原文

在 Activity.java 的源代码中,我看到了以下一些方法:

public View findViewById(int id) {
    return getWindow().findViewById(id);
}

以及getWindow方法的定义:

public Window getWindow() {
    return mWindow;
}

但按照以下规则:

Avoid Internal Getters/Setters

In native languages like C++ it's common practice to use getters (e.g. i = getCount()) instead of accessing the field directly (i = mCount). This is an excellent habit for C++, because the compiler can usually inline the access, and if you need to restrict or debug field access you can add the code at any time.

On Android, this is a bad idea. 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.

Without a JIT, direct field access is about 3x faster than invoking a trivial getter. With the JIT (where direct field access is as cheap as accessing a local), direct field access is about 7x faster than invoking a trivial getter. This is true in Froyo, but will improve in the future when the JIT inlines getter methods.

所以我想知道为什么android开发者不直接访问这个mWindow对象?如果当前android版本的JIT不能内联访问,getWindow().findViewById(id)会比mWindow.findViewById(id)花费更多的时间,findViewById是一个比较常用的方法。

最佳答案

首先:您无法访问它,因为它是私有(private)的。

为什么它是私有(private)的?

如您所说,直接访问成员更快。另一方面,您调用的方法不是很快,因为它会在 View 层次结构中查找某些 View 。因此,就执行该任务所需的总时间百分比而言,使用方法而不是直接访问会产生少量开销。

无论如何,我认为这是封装的原因。

您正在调用您不拥有的东西(即 Android SDK)。所以,你不应该对“另一边”发生的事情做出任何假设。只需使用此方法并期望它会返回您想要的 View (如果不存在则返回 null)。

也许下一个版本的 android 将使用不同的方法来查找 View ,而不是调用 getWindow()。如果您使用此方法,他们 (Google/Android) 可以简单地将此方法标记为已弃用并将您的调用“转发”到最新的实现。如果您直接调用 getWindow(),您可能会寻找不再放置在那里的东西。

关于android - 避免内部 getter/setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4537449/

有关android - 避免内部 getter/setter的更多相关文章

  1. ruby-on-rails - RSpec:避免使用允许接收的任何实例 - 2

    我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_

  2. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  3. ruby - 是否可以从也在该模块中的类内部调用模块函数 - 2

    在这段Ruby代码中:ModuleMClassC当我尝试运行时出现“'M:Module'的未定义方法'helper'”错误c=M::C.new("world")c.work但直接从另一个类调用M::helper("world")工作正常。类不能调用在定义它们的同一模块中定义的模块函数吗?除了将类移出模块外,还有其他解决方法吗? 最佳答案 为了调用M::helper,你需要将它定义为defself.helper;结束为了进行比较,请查看以下修改后的代码段中的helper和helper2moduleMclassC

  4. ruby - 无法安装 gem - make 未被识别为内部或外部命令可运行程序或批处理文件 - 2

    我想在Windows7上安装带有ruby​​1.9.3的rspec-railsgem。我收到一些错误消息,提示无法安装某些json库。所以,我使用下面的说明来解决它。来源=The'json'nativegemrequiresinstalledbuildtools从[rubyinstaller.org][3]下载[Ruby1.9.3][2]从[rubyinstaller.org][3]下载DevKit文件对于Ruby1.9.3,使用[DevKit-tdm-32-4.5.2-20110712-1620-sfx.exe][4]将DevKit解压到路径C:\Ruby193\DevKit运行cd

  5. ruby - 如何在 Ruby 中实现私有(private)内部类 - 2

    来自Java,我正在尝试在Ruby中实现LinkedList。我在Java中实现它的通常方法是有一个名为LinkedList的类和一个名为Node的私有(private)内部类,其中LinkedList的每个对象都作为Node对象。classLinkedListprivateclassNodeattr_accessor:val,:nextendend我不想将Node类暴露给外部世界。然而,通过Ruby中的这个设置,我可以使用这个访问LinkedList类之外的私有(private)Node类对象-node=LinkedList::Node.new我知道,在Ruby1.9中,我们可以使用

  6. ruby - Spec RSpec 模型属性 setter - 2

    我正在使用Sinatra(1.2)和RSpec(2.5),并希望创建一个具有属性TDD样式的新对象。最终结果应该是这样的:classUserdefinitialize(name)@name=nameendend我知道我必须在实现之前编写示例,但我想在这里解释我的问题。:)这是我目前无法使用的规范:describeUserit"createsanewuserobject"doname=mock("Aname")user=mock(User)#shouldn'tdothis,seethereply'suser.should_receive(:name=).with(name)User.ne

  7. ruby - 在 Ruby 中为变量赋值时如何避免控制台输出 - 2

    赋值时是否可以避免这种影响:irb(main):584:0>a=true=>trueirb(main):584:0>我有一个代码有很多赋值,当我试图测试它时,由于所有这些返回值,我看不到结果:truefalsetruefalsetruetrue.. 最佳答案 您可以启动irb或附加--noecho选项的控制台。$irb--noecho2.0.0p353:001>true2.0.0p353:002>否则,如果控制台由另一个进程启动,只需设置conf.echo=false$irb2.0.0p353:001>true=>true2.0.0

  8. ruby-on-rails - Rails 4.1 和 4.2 之间 ActiveRecord Setter 的区别? - 2

    我们将我们的应用程序从Rails4.1.14升级到4.2.5.1并遇到了以下问题:string="SomeString"ar_model=SomeArModel.newar_model.some_attribute=string#nextlineistruefor4.1,butfailsfor4.2ar_model.some_attribute.object_id==string.object_id显然,对象setter会复制每个对象(如果我有一个数组,里面的每个对象也会被复制),我想知道,这是不是有意为之并且是某些新安全功能的一部分?更新我将ruby​​-2.2.2p95用于两个ra

  9. ruby - Lisp 作为内部 Ruby DSL? - 2

    我已经能够找到:a)用Ruby编写的Lisp解释器(即外部DSL)http://onestepback.org/index.cgi/Tech/Ruby/LispInRuby.redb)作为RubyDSL的Prologhttp://www.kdedevelopers.org/node/2369c)讨论Ruby“作为”一个Lisphttp://www.randomhacks.net/articles/2005/12/03/why-ruby-is-an-acceptable-lisp但奇怪的是,我实际上找不到Lisp的“内部”实现,例如Prolog的实现。我只是不够谷歌,还是还没有人发表过这

  10. ruby-on-rails - Rails - 如何避免在 View 中使用 hidden_​​fields 将值传递给 Controller ​​? - 2

    有没有一种方法可以避免hidden_​​field方法将View中的值传递给Controller​​?出于安全原因,我更喜欢Controller方法。不幸的是,strong_parameters不支持值对@variables。EDIT6/181:00PMESTI'verenamedmygaragescontrollertoappointmentscars_controllernolongercreatesanewappointment(formallygarages).Anewappointmentiscreatedintheappointments_controller我目前的结构路

随机推荐