Java getter and setter methods become properties in Gosu

For methods on Java types that look like getters and setters, Gosu exposes methods on the type as properties rather than methods. Gosu uses the following rules for methods on Java types:

  • If the method name starts with set and takes exactly one argument, Gosu exposes this method as a setter for the property. The property name matches the original method but without the prefix set. For example, suppose the Java method signature is setName(String thename). Gosu exposes this method as a property set function for the property named Name of type String.
  • If the method name starts with get and takes no arguments and returns a value, Gosu exposes this method as a getter for the property. The property name matches the original method but without the prefix get. For example, suppose the Java method signature is getName() and it returns a String. Gosu exposes this method as a property get function for the property named Name of type String.
  • If the method name starts with is, takes no arguments, and returns a Boolean value, the rules are similar to the rules for get. Gosu exposes this method as a getter for the property. The property name matches the original method but without the prefix is. For example, suppose a Java method signature is isVisible(). Gosu exposes this method as a property get function for a Boolean property named Visible.

If the Java code has a setter and a getter, Gosu makes the property readable and writable. If the setter is absent, Gosu makes the property read-only. If the getter is absent, Gosu makes the property write-only.

For example, consider a Java class called Circle with the following method declarations:

public double getRadius() 
public void setRadius(double dRadius)

Gosu exposes these methods as the Radius property, which is readable and writable. With this property, you can use straightforward code such as:

circle.Radius = 5    // Property SET
print(circle.Radius) // Property GET

See also