【问题标题】:What should be the name of getter method for a boolean variable starting with `is` [duplicate]以`is`开头的布尔变量的getter方法的名称应该是什么[重复]
【发布时间】:2018-03-14 09:17:56
【问题描述】:

我有下面的成员变量mIsMobilePresent,其中以m 开头是我们遵循的约定来识别它是一个成员变量。

我的问题应该是这个变量的 getter 的名称是什么?

getIsMobilePresentgetIsMobilePresentisMobilePresent

【问题讨论】:

  • 你可以查看这个:stackoverflow.com/questions/5322648/…
  • 它应该是isIsMobilePresent(),就像如果布尔字段被称为island,它应该是isIsland()。但是你不应该将你的字段命名为isMobilePresent,因为它的类型已经表明它是一个布尔值。就像您不应该在字段前加上“m”一样,因为这已经是隐含的,任何半体面的 IDE 都会以不同的颜色或字体样式显示字段。只需遵循 Java 编码约定。

标签: java naming-conventions getter-setter


【解决方案1】:

首先,在Java中,我们通常不使用“m”前缀标记成员变量。

通常的(Java Bean)约定是变量被命名

boolean mobilePresent

和吸气剂

public boolean isMobilePresent

或者,可以使用has-前缀

public boolean hasConfiguredMobileConnections;

Where is the JavaBeans naming convention defined?

答案:Herehere

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    相关资源
    最近更新 更多