Java 为什么变量名通常以字母“m”开头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4237469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:52:56  来源:igfitidea点击:

Why do variable names often start with the letter 'm'?

javaandroidnaming-conventions

提问by Kallja

Looking at the Android tutorials such as the Notepad tutorial, I noticed that almost all variables are named starting with the letter 'm'. What convention is this, and where does it originate from?

查看 Android 教程,例如记事本教程,我注意到几乎所有变量都以字母“m”开头。这是什么约定,它起源于哪里?

采纳答案by Matthew Flaschen

It stands for member. I personally find this convention unhelpful, but it's subjective.

它代表会员。我个人认为这个约定没有帮助,但它是主观的。

回答by Vladimir Ivanov

'm' means member of the class. So, if you don't use IDE to highlight your members, then you will understand that it is a member by it's name

'm' 表示该类的成员。因此,如果您不使用 IDE 来突出显示您的成员,那么您将通过其名称了解它是一个成员

回答by swcai

'm' means the variable is a member variable of the class...

'm' 表示该变量是类的成员变量...

回答by stacker

As already answered this prefix indcates that a variable is member.

正如已经回答的那样,这个前缀表明一个变量是成员。

Somtimes people use other prefixes if you discover some variables starting with 'i' or 's' it could also be a variant of the Hungarian Notation

有时人们会使用其他前缀,如果您发现一些以“i”或“s”开头的变量,它也可能是匈牙利符号的变体

回答by chunkyguy

not only in java, I've seen similar convention in cocos2d+box2d samples where some of the variables start with m_, but others don't, very confusing.

不仅在 java 中,我在 cocos2d+box2d 示例中也看到了类似的约定,其中一些变量以 m_ 开头,但其他变量不以 m_ 开头,非常令人困惑。


b2World* world;
GLESDebugDraw *m_debugDraw;

I guess to differentiate C++ box2d variables from Obj-C variables.

我想将 C++ box2d 变量与 Obj-C 变量区分开来。

回答by Warren Chu

See Code Style Guidelines for Contributors: Follow Field Naming Conventions. The use of the "m" prefix is more specific that simply denoting a "member" variable: It's for "non-public, non-static field names."

请参阅贡献者的代码样式指南:遵循字段命名约定。“m”前缀的使用更具体,仅表示“成员”变量:它用于“非公共、非静态字段名称”。

回答by Klaus

The m is here to indicate a member variable.

m 在这里表示一个member 变量。

It has 2 huge advantages:

它有两个巨大的优势:

  • If you see it, you instantly recognize it as a member variable.
  • Press m and you get all members via the auto completer. (This one is not in the other answers)
  • 如果您看到它,您会立即将其识别为成员变量。
  • 按 m,您将通过自动完成程序获取所有成员。(这个不在其他答案中)

回答by Danilo Dughetti

According to Android source code documentation:

根据 Android 源代码文档

  • Non-public, non-static field names start with m.
  • Static field names start with s.
  • Other fields start with a lower case letter.
  • Public static final fields (constants) are ALL_CAPS_WITH_UNDERSCORES.
  • 非公共、非静态字段名称以 m 开头。
  • 静态字段名称以 s 开头。
  • 其他字段以小写字母开头。
  • 公共静态最终字段(常量)是 ALL_CAPS_WITH_UNDERSCORES。

Note that this is for writing Android source code. For creating Android apps, the Google Java Style Guidemay be more helpful.

请注意,这是用于编写 Android 源代码。对于创建 Android 应用程序,Google Java 风格指南可能更有帮助。