There is a "standard" pattern for getters and setters in Java, called Bean properties. Basically any method starting with get
, taking no arguments and returning a value, is a property getter for a property named as the rest of the method name (with a lowercased start letter). Likewise set
creates a setter of a void method with a single argument.
For example:
// Getter for "awesomeString"
public String getAwesomeString() {
return awesomeString;
}
// Setter for "awesomeString"
public void setAwesomeString( String awesomeString ) {
this.awesomeString = awesomeString;
}
Most Java IDEs will generate these methods for you if you ask them (in Eclipse it's as simple as moving the cursor to a field and hitting Ctrl-1, then selecting the option from the list).
For what it's worth, for readability you can actually use is
and has
in place of get
for boolean-type properties too, as in:
public boolean isAwesome();
public boolean hasAwesomeStuff();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…