Skip to main content

Overriding Triggers ?

1 reply [Last post]
pforhan
Offline
Joined: 2003-08-07
Points: 0

Copied from the old forums:
https://openjfx.dev.java.net/servlets/ProjectForumMessageView?messageID=...

>>Is it possible to override a Trigger behaviour of
>superclass
>
>I think so, as long as the trigger is calling a method.
>Methods can be overridden.
>
>Pat.

Hi Pat,

The following code shows that triggers are executed in sequence:
----------------------------------------------------
class A{ attribute attr: String; }

class B extends A{}

trigger on A.attr = newValue{
System.out.println("A class: {attr}");
}

trigger on B.attr = newValue{
System.out.println("B class: {attr}");
}

trigger on A.attr = newValue{
System.out.println("C class: {attr}");
}

var classB = B { attr: "b "};
------------- Output ------------------------------
A class: b
C class: b
B class: b
----------------------------------------------------

Is there any way to override a trigger behaviour?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
pforhan
Offline
Joined: 2003-08-07
Points: 0

> The following code shows that triggers are executed
> in sequence:
>...
> Is there any way to override a trigger behaviour?

Again, you can do this best by using an operation. Try this:

-------
class A{
attribute attr: String;
operation dump();
}

trigger on A.attr = newValue{
dump();
}

operation A.dump() {
println("A class: {attr}");
}

class B extends A {
}

operation B.dump() {
println("B class: {attr}");
}

class C extends B {
}

operation C.dump() {
println("C class: {attr}");
}

var classB = B { attr: "b "};
var classC = C { attr: "only C"};

-----------

Note that there is only one trigger, but that the appropriate dump() method is called for each object. This is the best way to acheive overriding.