Defining Classes
When building a class, you typically don't start from scratch—instead, you make the new class the child of an existing class. This is called creating a subclass. By subclassing a class, the new class will inherit all of the parent's methods and variables. This approach means that most of the difficult work will already be done by an existing breve class, and we can simply override and customize the behaviors that we need to.
For example, if we're building an object which will move through the 3D world, we'd like to have an object that understands the relationship between position, velocity and acceleration. Instead of implementing such a class ourselves, we can subclass Mobile.tz which is included with breve. Our custom subclass will contain the custom behaviors we desire, while the parent class takes care of the details.
When building a class, you must first decide the class name and its parent class. The parent class is the class from which the new class will inherit its behaviors. Classes which are to be used primarily for computation and do not require any special inherited behaviors, will typically use the generic root class Object. Classes which move around the world will inherit behaviors from Mobile, while immobile objects in the world will inherit behaviors from Stationary. A full list of classes is available in the appendix (???).
An empty class is simply defined by the following steve code:
parent_class_name:class_name{ }
Because we often deal with classes in their plural form (like when creating multiple instances of an object), it can be useful to give a class an alias which will allow us to refer to the class
in its plural form. This is not required but may make code easier to read. This alias is defined by adding the text (aka after the class name.
alias_name)
As an example of defining a class, both with and without an alias, consider a class called myMobile which will be a child of the class Mobile:
# first without the alias...
Mobile : myMobile {
}
# now with the alias...
Mobile : myMobile (aka myMobiles) {
}
This code above defines an empty class with no variables and no methods. This means that it will behave exactly as its parent class does. The next step is to customize the class's behavior by adding in methods and variables.
