Method Reference

 - 타입만 맞춰주면 내부적으로 연결된다.

인터페이스의 데이터 타입이 맞으면 동작을 보장.

ex)

btn.setListener(dialog::doOnClick); //  Method Reference - 타입만 맞춰주면 내부적으로 연결한다.

btn.setListener(System.out::println); 

Stream<Button1> stream = Arrays.stream(labels).map(Button1::new); // Construct Reference



  1. @FunctionalInterface
  2. interface IClickListener {
  3.         void onClick(String s);
  4. }
  5. class Button1 {
  6.         private List<IClickListener> listeners = new ArrayList<>();
  7.        
  8.         private String text;
  9.        
  10.         public Button1 (){}
  11.         public Button1(String s) {
  12.                 text = s;
  13.                 System.out.println(text);
  14.         }
  15.         public void setListener(IClickListener listener) {
  16.                 this.listeners.add(listener);
  17.         }
  18.         public void onClick() {
  19. //              for(IClickListener l : listeners) {
  20. //                      l.onClick("click");
  21. //              }
  22.                 listeners.forEach(System.out::println); // 동일 동작
  23.         }
  24. }
  25. class Dialog {
  26.         public void doOnClick(String text) {
  27.                 System.out.println(text);
  28.         }
  29. }
  30. @FunctionalInterface // 단일추상메소드 인터페이스임을 알림
  31. // 컴파일러가 메소드가 하나만 존재해야 한다는 것을 보장.
  32. interface IFoo { // 단일 인터페이스
  33.         void foo(String s, String s1);
  34. //      void goo(); // 두개는 선언 못한다.
  35.        
  36. }
  37.  
  38. @FunctionalInterface
  39. interface IGoo {
  40.         void goo();
  41. }
  42. public class Ex2 {
  43.         public static void goo(String s ) {
  44.                 System.out.println("goo" + s);
  45.         }
  46.         public static void main(String[] args) {
  47.                 IFoo foo = (s, s1) -> System.out.println(+ s1);
  48. //              IFoo foo = s -> System.out.println(s); // 인자가 하나인 경우 괄호가 생략 가능하다.
  49.                 foo.foo("d""c");
  50.                
  51.                 Button1 btn = new Button1();
  52.                 // 메소드 레퍼런스
  53.                 // 메소드를 맵핑 시킨다.
  54.                
  55.                 Dialog dialog = new Dialog();
  56.                 btn.setListener(dialog::doOnClick); // 타입만 맞춰주면 내부적으로 연결한다.
  57.                 btn.setListener(System.out::println);
  58.                
  59.                 btn.setListener(Ex2::goo);
  60.                
  61.                 btn.onClick();
  62.                
  63.                
  64.                 String[] labels = {"AAA","BBB","CCC"};
  65.                
  66.                 Stream<Button1> stream = Arrays.stream(labels).map(Button1::new); // Construct Reference
  67.                 stream.forEach(Button1::onClick);
  68.         }
  69. }


'java' 카테고리의 다른 글

Java 8 신기능 정리 - Stream  (0) 2014.07.02
Java 8 신기능 정리 - Default Method  (0) 2014.07.02
Java 8 신기능 정리 - Lambda Expression  (0) 2014.07.01
Java7 의 신기능 정리  (0) 2014.07.01
Java 7 try catch 문법. autocloseable  (0) 2014.06.20

+ Recent posts