Java Reference
In-Depth Information
class, called MyClassExtended , extends MyClass and overrides name with a bind
and a trigger too.
class MyClass {
public var foo:String = "Jim";
public var name: String = bind foo on replace {
println("MyClass: name={name}");
};
}
class MyClassExtended extends MyClass {
public var bar:String = "Eric";
override var name = bind bar on replace {
println("MyClassExtended: name={name}");
};
}
var m2 = MyClassExtended {};
m2.bar = "Joe";
This is the output when the block is executed:
MyClass: name=
MyClassExtended: name=
MyClass: name=Eric
MyClassExtended: name=Eric
MyClass: name=Joe
MyClassExtended: name=Joe
From this example, we can glean that triggers and bound variables, when over-
ridden, behave quite differently. To sum it up:
Overridden triggers are cumulative . When an instance variable with a
trigger is overridden by a superclass with another trigger, the original trigger
is not hidden. It also gets executed along with the overridden trigger. Based
on the output, the base class trigger will be executed first, followed by the
superclass trigger(s).
Overridden bound variables are hidden. A bound instance variable in a
superclass will be re-evaluated by the overridden subclass instance variable.
In our example, when MyClassExtended is instantiated, the value of its name
instance variable is the same for both the MyClassExtended subclass object
and the MyClass base object.
Search WWH ::




Custom Search