-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodRef3.java
More file actions
30 lines (22 loc) · 1.15 KB
/
Copy pathMethodRef3.java
File metadata and controls
30 lines (22 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package methodref;
import java.util.function.Function;
public class MethodRef3 {
public static void main(String[] args) {
// 임의 객체의 인스턴스 메서드 참조(특정 타입의)
Person person1 = new Person("Kim");
Person person2 = new Person("Park");
Person person3 = new Person("Lee");
// 람다
Function<Person, String> fun1 = (Person person) -> person.introduce();
System.out.println("person1 = " + fun1.apply(person1));
System.out.println("person2 = " + fun1.apply(person2));
System.out.println("person3 = " + fun1.apply(person3));
System.out.println();
// 메서드 참조, 타입이 첫 번째 매개변수가 됨
// 그리고 첫 번쨰 매개변수의 메서드를 호출, 나머지는 순서대로 매개변수에 전달
Function<Person, String> fun2 = Person::introduce; // 타입::인스턴스 메서드 (타입 이름) -> 이름.메서드
System.out.println("person1 = " + fun2.apply(person1));
System.out.println("person2 = " + fun2.apply(person2));
System.out.println("person3 = " + fun2.apply(person3));
}
}