-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambdaEx1.java
More file actions
38 lines (28 loc) · 852 Bytes
/
LambdaEx1.java
File metadata and controls
38 lines (28 loc) · 852 Bytes
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
34
35
36
37
38
package lambdaEx;
@FunctionalInterface
interface MyFunction {
void run(); // public abstract void run();
}
public class LambdaEx1 {
static void execute(MyFunction f) { // 매개변수의 타입이 MyFunction인 메서드
f.run();
}
static MyFunction getMyFunction() { // 반환타입이 MyFunction인 메서드
return () -> System.out.println("f3.run()");
}
public static void main(String[] args) {
// 람다식으로 MyFunction의 run을 구현
MyFunction f1 = () -> System.out.println("f1.run()");
MyFunction f2 = new MyFunction() { // 익명 클래스로 run()을 구현
public void run() { // public을 반드시 붙여야함
System.out.println("f2.run()");
}
};
MyFunction f3 = getMyFunction();
f1.run();
f2.run();
f3.run();
execute(f1);
execute(() -> System.out.println("run()"));
}
}