Properties

Properties are dynamic and virtual functions that behave like data. Gosu classes can define properties to provide access to a property value similarly to variables on the class. Other objects use the intuitive syntax of the period symbol (.) to access the properties. You can use Gosu code to implement get and set functionality for a property. The simplest code that gets or sets a property value uses an instance variable. You can also implement properties by writing getter and setter methods.

Getting and setting properties

To get and set properties on an object, use the period symbol just as for getting and setting standard variables. For example, the following code sets and gets properties on a MyClass object that has Field1 and Field2 properties:

// Create a new class instance
var a = new MyClass()

// Set a property value
a.Field1 = 5

// Get a property value
print(a.Field2)

Static properties

You use static properties directly on the class without creating an instance of the class.

For example, if the MyClass class has a static property, BaseURL, you access its value as shown in the following code:
var myURL = MyClass.BaseURL + "/destination"
The following lines are invalid:
// Create a new class instance
var b = new MyClass()

// Attempt to access a static property on the instance
myURL = b.BaseURL + "/destination" // This line does not compile

See also