mSomeVar 名称 - 从哪里来以及为什么?
Android 源中的变量以 m
为前缀,例如 mStream
或 mHeaderValueIndex
。匈牙利表示法背后的基本原理是什么?
Variables in Android sources are prefixed with m
, like mStream
or mHeaderValueIndex
. What's the rationale behind Hungarian notation here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“m”是成员的缩写。这只是让您知道您正在处理类成员变量的一种方式。在其他环境或编程语言中,您可以使用下划线前缀。
The "m" is short for member. It's just a way of letting you know you're dealing with a class member variable. In other environments or programming languages, you might use an underscore prefix instead.
在属性名称上添加“m”或“m_”或“_”等前缀的基本原理是使它们更容易与局部变量和参数区分开。
但所有 IDE(以及许多文本编辑器)都能够使用不同的颜色来呈现属性名称和本地名称或参数名称,这使得这种约定现在几乎是浪费时间。您会注意到 Sun Java 风格指南并没有“祝福”这一约定。
但谁知道他们为什么选择在 Android 代码库中这样做。也许他们正在尝试协调 Java、C 和 C++ 编码标准?也许谷歌程序员都使用黑白显示器? :-)
The rationale for putting prefixes like "m" or "m_" or "_" on attribute names is to make them easier to distinguish from local variables and parameters.
But all IDEs (and a lot of text editors) are capable of using different colours to render attribute names and local or parameter names, which makes this convention is pretty much a waste of time these days. And you will notice that the Sun Java Style Guide does not "bless" this convention.
But who knows why they chose to do this in the Android codebase. Perhaps they were trying to align their Java, C and C++ coding standards? Perhaps Google coders all use black-and-white monitors? :-)