Android的记事本练习1教程和Menu.FIRST [英] Android Notepad Exercise 1 tutorial and Menu.FIRST

查看:207
本文介绍了Android的记事本练习1教程和Menu.FIRST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

道歉,如果这最终是一个愚蠢的问题,但我只是想知道这是为什么在记事本练习1教程的第9步进行(的 http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html )...

Apologies if this ends up being a stupid question, but I was just wondering why this is done on step 9 of the Notepad Exercise 1 tutorial (http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html) ...

public static final int INSERT_ID = Menu.FIRST;

...然后 INSERT_ID 使用无处不在,而不是仅仅使用 Menu.FIRST ,而不是直接无处不在?

... and then INSERT_ID is used everywhere, instead of just using Menu.FIRST directly instead everywhere?

我在想,我不希望创建任何新的变量,如果我没有,尤其是当它们是静态的最后了。

I was thinking that I don't want to create any new variables if I don't have to, especially when they are static final already.

是它昂贵创建 INSERT_ID

推荐答案

因为如果你使用Menu.First处处然后决定把该选项的菜单,以便它不再是你需要更新的第一个项目的所有引用。这样,你只需要在一个地方和更理智命名INSERT_ID更新将反映你已经在其他地方的变化。

Because if you used Menu.First everywhere then decided to move that option in the menu so that it was no longer the first item you would need to update all the references. This way you only need to update it in one place and the more sensibly named INSERT_ID will reflect the changes that you have made everywhere else.

作为创建新的变量的性能损失:是的,创建变量将占用的CPU时间(不显着)量,是存储变量占用的内存(不显着)金额,但你永远不应该把code可读性 - 之前的表现,直到你已经确定你已经走到一个瓶颈 - 你会在这个非常受限制的环境是附近的一个问题,什么

As for the performance hit of creating new variables: Yes, creating the variable will use up a (insignificant) amount of CPU time, and yes storing the variable will use up an (insignificant) amount of memory, but you should never put performance before code readablity until you have determined that you have hit a bottleneck - you'd be in a very restricted environment for this to be anything near a problem.

最后,大量的编译器将内联引用反正INSERT_ID。这意味着对INSERT_ID所有引用在编译时将与Menu.First值替换和变量将实际上从未被创建。我不知道有足够的了解Android的编译器肯定的一种方式或其他的说法,但我会感到惊讶,如果没有做到这一点。

Finally, a lot of compilers will inline the references to INSERT_ID anyway. This means that all references to INSERT_ID will be replaced at compile time with the value in Menu.First and the variable will never actually be created. I don't know enough about the Android compiler to say for sure one way or the other, but I would be surprised if it didn't do this.

这篇关于Android的记事本练习1教程和Menu.FIRST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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