为什么变量名通常以字母“m”开头?(复制)

时间:2022-05-18 23:14:44

This question already has an answer here:

这个问题已经有了答案:

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”开头的。这是什么习俗?它从何而来?

8 个解决方案

#1


246  

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

它所代表的成员。我个人觉得这个惯例没有用,但它是主观的。

#2


97  

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”前缀更具体地表示一个“成员”变量:它表示“非公共的、非静态字段名”。

#3


63  

According to Android source code documentation:

根据Android源代码文档:

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

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

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

#4


29  

The m is here to indicate a member variable.

m表示一个成员变量。

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,你通过自动完成器得到所有成员。(其他答案中没有这个)

#5


14  

'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来突出显示成员,那么您将理解它是由其名称组成的成员

#6


5  

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”开头,人们会用其他前缀,这也可能是匈牙利符号的变体

#7


2  

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

“m”表示变量是类的成员变量。

#8


0  

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_开头,而另一些则不是,非常混乱。


b2World* world;
GLESDebugDraw *m_debugDraw;

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

我猜是为了区分c++ box2d变量和object -C变量。

#1


246  

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

它所代表的成员。我个人觉得这个惯例没有用,但它是主观的。

#2


97  

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”前缀更具体地表示一个“成员”变量:它表示“非公共的、非静态字段名”。

#3


63  

According to Android source code documentation:

根据Android源代码文档:

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

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

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

#4


29  

The m is here to indicate a member variable.

m表示一个成员变量。

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,你通过自动完成器得到所有成员。(其他答案中没有这个)

#5


14  

'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来突出显示成员,那么您将理解它是由其名称组成的成员

#6


5  

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”开头,人们会用其他前缀,这也可能是匈牙利符号的变体

#7


2  

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

“m”表示变量是类的成员变量。

#8


0  

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_开头,而另一些则不是,非常混乱。


b2World* world;
GLESDebugDraw *m_debugDraw;

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

我猜是为了区分c++ box2d变量和object -C变量。