Created
January 23, 2013 04:32
-
-
Save natp0ng/4601953 to your computer and use it in GitHub Desktop.
Callback in class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The callback interface | |
interface MyCallback { | |
void callbackCall(); | |
} | |
// The class that takes the callback | |
class Worker { | |
MyCallback callback; | |
void onEvent() { | |
callback.callbackCall(); | |
} | |
} | |
// Option 1: | |
class Callback implements MyCallback { | |
void callbackCall() { | |
// callback code goes here | |
} | |
} | |
worker.callback = new Callback(); | |
// Option 2: | |
worker.callback = new MyCallback() { | |
void callbackCall() { | |
// callback code goes here | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment