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
| interface Compute {
abstract int min(int x,int y);
//abstract int max(int x,int y); //인터페이스의 메소드가 2개 이상이면 람다식을 사용할 수 없다.
}
public class LambdaExample {
public static void main(String[] args) {
//익명클래스 구현
Compute compute1 = new Compute() {
@Override
public int min(int x, int y) {
return x<y?x:y;
}
//메소드가 2개이상이면 익명클래스는 추가할 수 있다.
/*
@Override
public int max(int x, int y) {
return x>y?x:y;
}
*/
};
//람다식
Compute compute2 = (x,y) -> {return x<y?x:y; };
System.out.println(compute1.min(5,10));
System.out.println(compute2.min(8,10));
}
}
|
댓글남기기