代码之家  ›  专栏  ›  技术社区  ›  Abi

生成不带下划线(不是前缀/后缀)的Getter和setter+eclipse

  •  2
  • Abi  · 技术社区  · 12 年前

    我的变量是

    private String category_code = null;
    

    我的getter和setter生成为

    public String getCategory_code() {
            return category_code;
        }
        public void setCategory_code(String category_code) {
            this.category_code = category_code;
        }
    

    是否可以生成

    public String getCategorycode() {
            return category_code;
        }
        public void setCategorycode(String categorycode) {
            this.category_code = category_code;
        }
    

    我选中了“属性”-->代码样式-->字段,但这仅用于前缀和后缀。

    还是应该将变量重命名为m_categoryCode?并得到如下输出?

    public String getCategoryCode() {
            return m_categoryCode;
        }
        public void setCategoryCode(String categoryCode) {
            m_categoryCode = categoryCode;
        }
    

    哪个更好?

    2 回复  |  直到 12 年前
        1
  •  3
  •   sword-yang    12 年前

    getter和setter方法的名称是从字段名称派生出来的。如果为字段(例如fValue、_value、val_m)使用前缀或后缀,则可以在“代码样式”首选项页面(Windows>首选项>Java>代码样式)中指定后缀和前缀。

    参考号: here

        2
  •  1
  •   jtahlborn    12 年前

    Java代码倾向于遵循camelCaseStyle,而不是c_undercore_style。遵循现有的标准通常会在各种方面对您有所帮助(您将能够更好地阅读他人的代码,而其他人将能够更好的阅读您的代码,其中“其他人”是同一语言的其他开发人员)。此外,该语言的工具往往能更好地工作(这是一个很好的例子)。