Advertisement
ganryu

Explicación Java 8

Mar 5th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. // Esto
  2. EventQueue.invokeLater(new Runnable() {
  3.     public void run() {
  4.         try {
  5.             NaveController controladorNave = new NaveController();
  6.             controladorNave.initialize();
  7.         } catch (Exception e) {
  8.             e.printStackTrace();
  9.         }
  10.     }
  11. });
  12.  
  13. // Se puede convertir en
  14. EventQueue.invokeLater(() -> {
  15.     try {
  16.         NaveController controladorNave = new NaveController();
  17.         controladorNave.initialize();
  18.     } catch (Exception e) {
  19.         e.printStackTrace();
  20.     }
  21. });
  22.  
  23. /**
  24.  * Cuando una interfaz (como Runnable) tiene un único método
  25.  * puedes reemplazar la clase que implementa la interfaz por
  26.  * ("parametros del método) -> { código del método }
  27.  *
  28.  * Por ejemplo si tenemos la interfaz
  29.  * @FunctionalInterface
  30.  * public interface Posicion {
  31.  *     public abstract Punto actualizaPosicion(Punto p);
  32.  * }
  33.  *
  34.  * y un método en una clase que recibe un objeto que implementa Posicion
  35.  * Punto this.punto = new Punto(5, 6);
  36.  * public abstract void mover(Posicion pos) {
  37.  *     this.punto = pos.actualizaPosicion(this.punto);
  38.  * }
  39.  *
  40.  * Podemos llamar al método haciendo
  41.  * mover((this.punto) -> {
  42.  *     this.punto.x += 1;
  43.  *     this.punto.y += 1;
  44.  * }
  45.  *
  46.  * Esto es lo mismo que
  47.  * mover(new Posicion() {
  48.  *     public void mover(this.punto) {
  49.  *         this.punto.x += 1;
  50.  *         this.punto.y += 1;
  51.  *     }
  52.  * });
  53.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement