Defining and using properties on interfaces

Interfaces created in Gosu can declare properties by specifying explicit property get or property set accessors in interfaces with the following syntax:

property get Description() : String

Classes can implement an interface property with the explicit property get or property set syntax.

For example, the following code defines an interface:

package example

interface MyInterface {
  property get VolumeLevel() : int 
  property set VolumeLevel(vol : int) : void
}

A class could implement this interface with the following code:

class MyStereo implements MyInterface {
  var _volume : int

  property set VolumeLevel(vol : int) {
    _volume = vol
  }

  property get VolumeLevel() : int {
    return _volume
  }
}

After creating the class and interface, you can test the following code in the Gosu Scratchpad:

uses example.MyStereo

var v = new MyStereo()
v.VolumeLevel = 11
print("The volume goes to " + v.VolumeLevel)

This code prints:

The volume goes to 11

Alternatively, a class implementing a property can implement the property with the variable alias syntax by using the as keyword. Using this language feature, you make set methods that use a class instance variable to store property values, and get methods to get property values from the variable.

For example, the following, more concise code is functionally identical to the previous example implementation of MyStereo:

uses example.MyStereo

class MyStereo implements MyInterface {
  var _volume : int as VolumeLevel
}

If you run the same Gosu Scratchpad code as previously, the code prints the same results.

Interface methods that look like properties

If an interface’s methods look like properties, a class that implements the interface must implement those methods in Gosu as a property by using property get or property set syntax. If the interface contains a method whose name starts with "get" or "is" and takes no parameters, define the method by using the Gosu property syntax. See earlier in this topic for examples.

See also