-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodeRefEx2.java
More file actions
33 lines (23 loc) · 1.29 KB
/
Copy pathMethodeRefEx2.java
File metadata and controls
33 lines (23 loc) · 1.29 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
31
32
33
package methodref;
import java.util.function.Function;
import java.util.function.Supplier;
public class MethodeRefEx2 {
public static void main(String[] args) {
// 1. 정적 메서드 참조
Function<String, String> staticMethod1 = (name) -> Person.greetingWithName(name);
Function<String, String> staticMethod2 = Person::greetingWithName;
System.out.println("staticMethod1 = " + staticMethod1.apply("Kim"));
System.out.println("staticMethod2 = " + staticMethod2.apply("Kim"));
// 2. 특정 객체의 인스턴스 참조
Person person = new Person("Choi");
Function<Integer, String> instanceMethod1 = (n) -> person.introduceWithNumber(n);
Function<Integer, String> instanceMethod2 = person::introduceWithNumber; // 객체::인스턴스메서드
System.out.println("instanceMethod1 = " + instanceMethod1.apply(1));
System.out.println("instanceMethod2 = " + instanceMethod2.apply(2));
// 3. 생성자 참조
Function<String, Person> newPerson1 = (name) -> new Person(name);
Function<String, Person> newPerson2 = Person::new; // 클래스::new
System.out.println("newPerson1 = " + newPerson1.apply("Choi"));
System.out.println("newPerson2 = " + newPerson2.apply("Choi"));
}
}