Object Oriented Programming
Classes
We will quickly introduce the structure of classes that you might already be familiar with if you've done some OO programming before :
package my.pack; /* this will define the class my.pack.MyClass */ class MyClass { // .... }
A Class can have several variables and methods.
package my.pack; class MyClass { var id : Int; static var name : String = "MyString"; function foo() : Void { } static function bar( s : String, v : Bool ) : Void { } }
Variables and methods can have the following flags :
- static : the field belongs to the Class itself and not to instances of this class. Static identifiers can be used directly in the class itself. Outside of the class, it but must be used with the class name (for example :
my.pack.MyClass.name).
- public : the field can be accessed by other classes. By default, all fields are
private. - private : the field access is restricted to the class itself and all the classes that will
extendsthis class. This is better to ensure that the class internal state is not accessible.
All class variables must be declared with a type (you can use Dynamic if you don't know which type to use). Function arguments and return types are optional but are still stricly checked as we will see when introducing type inference.
Static variables can have an initial value although it's not required.
Constructor
The class can have only one constructor, which is the not-static function called new. This is a keyword that can also be used to name a class function :
class Point { public var x : Int; public var y : Int; public function new() { this.x = 0; this.y = 0; } }
Constructor parametrization & overloading :
public function new (x : Int, ?y : Int){ this.x = x; this.y = (y == null) ? 0 : y; // "y" is optional }
Class Inheritance
When declared, it's possible that a class extends one class and implements several classes or interfaces. This means the class will inherit several types at the same time, and can be treated as such. For example :
class D extends A, implements B, implements C { }
Every instance of D will have the type D but you will also be able to use it where an instance of type A, B or C is required. This means that every instance of D also has the types A , B and C.
Extends
When extending a class, your class inherits from all public and private not-static fields. You can then use them in your class as if they where declared here. You can also override a method by redefining it with the same number of arguments and types as its superclass. Your class can not inherit static fields.
When a method is overridden, then you can still access the superclass method using super :
class B extends A { function foo() : Int { return super.foo() + 1; } }
In your class constructor you can call the superclass constructor using also super :
class B extends A { function new() { super(36,""); } }
Implements
When implementing a class or an interface, your class is required to implement all the fields declared or inherited by the class it implements, with same type and name. However the field might already be inherited from a superclass.
Interfaces
Interface are classes prototypes. They are declared using the interface keyword. By default, all interfaces fields are public. Interfaces cannot be instantiated.
interface PointProto { var x : Int; var y : Int; function length() : Int; }
An interface can also implements one or several interfaces :
interface PointMore implements PointProto { function distanceTo( p : PointProto ) : Float; }
«« Type Inference - Type Parameters »»