-
Notifications
You must be signed in to change notification settings - Fork 103
Class, Interface, and Mixin
JSX adopts a single-root, single-inheritance class model with support for interfaces and mixins.
A class in JSX have following characteristics.
- all classes derive from the built-in Object class
- single inheritance using the extends keyword
- more than one interfaces and mixins can be implemented using the implements keyword
For example, "class E extends A implements C, D" means: C can override members of A (*1), D can override members of the production of *1 (*2), and that E can override members of the production of *2.
A class may have the following attributes set.
Name | Description |
---|---|
abstract | a class with abstract methods should have the attribute set |
final | declares that the class may not be extended |
An user-defined class may have zero ore more functions or variables.
The example below defines a class called Human with a member variable _name, constructor function that takes a string as an argument, and a non-static member function (i.e. method) called say, that does not take any arguments and returns void).
class Human {
var _name : string;
function constructor(name : string) {
this._name = name;
}
function say() : void {
log "My name is " + this._name;
}
}
A member function is defined by either of the following form. Arguments should have their types being annotated. The type after the closing parenthesis at the end of the argument list designates the return type of the function. Function that is not attributed abstract should have body. Function that is attributed abstract may have body.
// function with body
attribute* function funcname([arg1 : type[, arg2 : type, ...]]) : type { statement* }
// function without body
attribute* function funcname([arg1 : type[, arg2 : type, ...]]) : type;
A function may have the following attributes set.
Name | Description |
---|---|
abstract | declares that the function should be overridden by a class that extends the class |
final | declares that the function may not be overridden |
static | declares that the function is static |
override | declares that the definition of the function is overriding an definition in the extended class (or implemented interface or mixin). A function overriding an existing function should have the attribute set |
A member variable is defined in the following form. For ordinary variables, either the type or the expression should be specified. For constants, expression is mandatory and type is optional.
// ordinary variable
attribute* var varname [: type][ = expression];
// constant
static const CONSTNAME [: type] = expression;
An ordinary member variable may have the following attributes set.
Name | Description |
---|---|
abstract | declares that the variable should be defined in a class that extends the class |
static | declares that the variable is static |
Member functions and variables can be referred to by applying the "." operator against a class name (for static members) or against a variable of the class type (or this).
// refers to a static variable
_Klass_.static_var
// calls a static function
_Klass_.static_func(...)
// refers to a non-static variable
_instance_.var
this.var // only within a non-static function
// calls a non-static function (i.e. method)
_instance_.func()
this.func() // only within a non-static function
An interface in JSX is similar to that of Java, except for the fact that it may have abstract variables. All the non-static functions are implicitly marked as abstract, and they cannot have bodies. An interface may implement other intefaces.
Mixin is provided so that implementations can be shared between classes. Mixins may implement interfaces or other mixins. It may have functions with body or non-abstract variables.