Code Injection in Custom Methods
[Previous]  [Next] 
|
User Manual [Previous]  [Next] Code Injection in Custom MethodsMuch of the usefulness of code injection is for generated methods, but it is also possible to inject code into custom methods, using the custom keyword. Example// This illustrates separate code injection // for custom and generated methods // The output will be as follows // Sound of beep // Warning // ... this is injected after the warning // Sound of beep // Warning // ... this is injected after the warning // Sound of beep // Warning // ... this is injected after the warning // class Car { queued radio { on { radioToggle -> off; } off { radioToggle -> on; } } after generated radioToggle* {soundBeep();} void soundBeep() { System.out.println("Sound of beep"); radioToggleWarning(); } void radioToggleWarning() { System.out.println("Warning"); } after custom radioToggle* { System.out.println( " ... this is injected after the warning"); } public static void main(String[] args) { Car x = new Car(); x.radioToggle(); x.radioToggle(); x.radioToggle(); } } Load the above code into UmpleOnline |