Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package nestedlocalanonymouslambdaevents;
- /**
- *
- * @author Kristiqn
- */
- public class NestedLocalAnonymousLambdaEvents {
- private static class NestedListener implements MyEventHandler{
- @Override
- public void happend(Object sender) {
- System.out.println("Something happaned.Event handler in my Nested class.");
- }
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // 0. conventional approach with a class
- Notifier notifier = new Notifier();
- notifier.doSomething();
- notifier.onSomething = new Listener();
- notifier.doSomething();
- // 1. Nested class
- notifier.onSomething = new NestedListener();
- notifier.doSomething();
- // 2. Local class
- class LocalListener implements MyEventHandler {
- @Override
- public void happend(Object sender) {
- System.out.println("Something happaned.Event handler in my Local class.");
- }
- }
- notifier.onSomething = new LocalListener();
- notifier.doSomething();
- // 3. Anonymous class
- notifier.onSomething = new MyEventHandler() {
- @Override
- public void happend(Object sender) {
- System.out.println("Somethign happaned. Event handler in my anonymous class.");
- }
- };
- notifier.doSomething();
- // 4. Lambda
- notifier.onSomething =
- (Object object) -> System.out.println("Somethign happaned. Event handler lambda expression");
- notifier.doSomething();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement