Named inner classes

You can define a named class within another Gosu class. This inner class can be used within the outer class that contains the definition, or from classes that extend the outer class.

Example

The following code demonstrates a simple inner class.

package example

 class Greeting {
  // function in the main (outer) class
  public function greet() {
    var x = new FrenchGreeting()
    print(x.sayWhat())
  }

   // INNER CLASS DECLARATION
  public class FrenchGreeting {
    public function sayWhat() : String {
      return "Bonjour"
    }
  }
}

Create the classes, and then try the following lines in Gosu Scratchpad to test the inner class.

var f = new example.Greeting()
f.greet()

This code prints:

Bonjour