Classes
Basic class
class SampleClass // create a class
{
// static member variable (can be read from both class definition and instance)
global StaticVariable = 5;
// the default construction function
function __construct( x )
{
// instance member variable (can be read only from class instance, not definition)
this.c = x;
}
// static construction function
function Create( x )
{
return new SampleClass( x );
}
function SampleMethod( a, b ) // "SampleClass.SampleMethod" is the name that will show up in debug output
{
return a + b + this.c; // usage of "this" turns the function into a method
}
function SampleFunction( a, b )
{
return a + b;
}
}
// instances of class
inst1 = new SampleClass( 123 );
inst2 = SampleClass.Create( 456 );
// inspect instance 1, instance 2 and the underlying interface of instance 1
printvar( inst1, inst2, metaobj_get( inst1 ) );Inheritance
// class A - base class
class A
{
function func()
{
println( "base" );
}
}
// class B - inherits from A
class B : A
{
function func()
{
this!A.func();
println( "override" );
}
}
instA = new A();
instB = new B();
instA.func(); // prints "base"
instB.func(); // prints "base", then "override"Overloadable operators/interfaces (Metamethods):
- basic math: +, - (both unary and binary), *, /, %
- comparison
- conversions to
boolandstring - typeof string retrieval
- cloning
- function call
