|
Spec-Zone .ru
спецификации, руководства, описания, API
|
functionDefinition and variableDeclaration are covered in their respective chapters. The remaining class members are covered below.
The initBlock is an optional block of code which is executed as the final step of instance initialization.
The postInitBlock is an optional block of code which is executed after instance initialization has completed.
A variableOverrideDeclaration allows changing the default value of a subclass' instance var and/or adding an onReplaceClause for that var. Either an initializingExpression or an onReplaceClause or both must be present. Note that while an initializingExpression overrides the default value, an onReplaceClause adds a block to be executed on update -- any onReplaceClause defined in a subclass is also executed on change to the variable -- this is critical for maintaining subclass invariants..
You can change the definition of an instance function in a subclass by overriding the function, using the override modifier.
Here we override the definition of the function greeting in the subclass Sub:
class Base {
function greeting() : Void {
println("Hi")
}
}
class Sub extends Base {
override function greeting() : Void {
println("Howdy")
}
}
def b = Base {};
def s = Sub {};
b.greeting();
s.greeting();
This prints:
Hi Howdy
Java methods can also be overridden, For example, to change how an object is printed its toString() method can be overridden:
class Point {
var x : Number;
var y : Number;
override function toString() : String {
"Point {x}, {y}"
}
}
def p = Point {x: 4.6 y: 8.9 };
println( p );
This prints
Point 4.6, 8.9