问题

例如。

boolean isCurrent = false;

你用它的吸气剂和制定者命名的是什么?


#1 热门回答(174 赞)

假设你有

boolean active;

访问者的方法是

public boolean isActive(){return this.active;}

public void setActive(boolean active){this.active = active;}

另请参见- Java编程/ Java Bean

  • Java编程语言的代码约定

#2 热门回答(55 赞)

http://geosoft.no/development/javastyle.html#Specific>前缀应该用于布尔变量和方法。 isSet,isVisible,isFinished,isFound,isOpen这是Sun用于Java核心包的布尔方法和变量的命名约定。使用is前缀解决了选择状态或标志等错误布尔名称的常见问题。 isStatus或isFlag根本不适合,程序员被迫选择更有意义的名字。布尔变量的setter方法必须设置前缀,如:void setFound(boolean isFound); is前缀有一些替代方案,在某些情况下更适合。这些是has,can和should前缀:boolean hasLicense(); boolean canEvaluate(); boolean shouldAbort = false;


#3 热门回答(47 赞)

对于名为isCurrent的字段,正确的getter / setter命名是setCurrent()/isCurrent()(至少是Eclipse认为的那样),这非常令人困惑,可以追溯到主要问题:

你的字段不应该首先被称为isCurrent.Isis一个动词和动词不适合表示一个Object的状态。改为使用形容词,突然间你的getter / setter名称会更有意义:

private boolean current;

public boolean isCurrent(){
    return current;
}

public void setCurrent(final boolean current){
    this.current = current;
}

原文链接