Eg.

boolean isCurrent = false;

你怎么命名它的getter和setter?


当前回答

Setter: public void setCurrent(boolean val)
Getter: public boolean getCurrent()

对于布尔值,也可以使用

public boolean isCurrent()

其他回答

Setter: public void setCurrent(boolean val)
Getter: public boolean getCurrent()

对于布尔值,也可以使用

public boolean isCurrent()

http://geosoft.no/development/javastyle.html#Specific

is prefix should be used for boolean variables and methods. isSet, isVisible, isFinished, isFound, isOpen This is the naming convention for boolean methods and variables used by Sun for the Java core packages. Using the is prefix solves a common problem of choosing bad boolean names like status or flag. isStatus or isFlag simply doesn't fit, and the programmer is forced to chose more meaningful names. Setter methods for boolean variables must have set prefix as in: void setFound(boolean isFound); There are a few alternatives to the is prefix that fits better in some situations. These are has, can and should prefixes: boolean hasLicense(); boolean canEvaluate(); boolean shouldAbort = false;

假设你有

boolean active;

Accessors方法

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

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

另请参阅

Java编程/Java Beans Java编程语言的代码约定

作为一个传达员,你可以:

// setter
public void beCurrent(boolean X) {
    this.isCurrent = X;
}

or

// setter
public void makeCurrent(boolean X) {
    this.isCurrent = X;
}

我不确定这些命名对以英语为母语的人是否有意义。

我相信会是:

void setCurrent(boolean current)
boolean isCurrent()