关于android变量命名前为什么加m的问题

关于这个问题,应该很多人都有这个疑问,命名规范项目里用的都是驼峰命名规则,但是之前看项目代码里安卓的变量很多前面会带个m,刚开始敲安卓代码的时候我看别人都这么写,我也就这么写了,一直到现在。

今天想知道一下为什么这么写,因为看安卓源码中,有的变量带m,有的变量不带m,终于在stack overflow上找到了答案,链接如下,Android变量前命名加m
在这里插入图片描述

Follow field naming conventions

  • 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开头。
其他字段以小写字母开头。
静态变量最终字段(常量)全大写。

谷歌官方给出的代码例子:

public class MyClass {
    public static final int SOME_CONSTANT = 42;
    public int publicField;
    private static MyClass sSingleton;
    int mPackagePrivate;
    private int mPrivate;
    protected int mProtected;
}

附录:安卓开发源代码编写规则:
https://source.android.com/setup/contribute/code-style

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章