2015-05-04 02:57:38 +00:00
|
|
|
public class InvokeDynamic {
|
2015-08-06 19:24:06 +00:00
|
|
|
private final int foo;
|
|
|
|
|
|
|
|
private InvokeDynamic(int foo) {
|
|
|
|
this.foo = foo;
|
|
|
|
}
|
|
|
|
|
2015-08-05 21:55:52 +00:00
|
|
|
private interface Operation {
|
|
|
|
int operate(int a, int b);
|
|
|
|
}
|
2015-05-04 02:57:38 +00:00
|
|
|
|
2015-08-05 21:55:52 +00:00
|
|
|
private static void expect(boolean v) {
|
|
|
|
if (! v) throw new RuntimeException();
|
|
|
|
}
|
|
|
|
|
2015-05-04 02:57:38 +00:00
|
|
|
public static void main(String[] args) {
|
2015-08-05 21:55:52 +00:00
|
|
|
int c = 4;
|
|
|
|
Operation op = (a, b) -> a + b - c;
|
2015-08-06 19:24:06 +00:00
|
|
|
expect(op.operate(2, 3) == (2 + 3) - 4);
|
|
|
|
|
2015-08-06 23:22:14 +00:00
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
new InvokeDynamic(i).test();
|
|
|
|
}
|
2015-08-06 19:24:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void test() {
|
|
|
|
int c = 2;
|
|
|
|
Operation op = (a, b) -> ((a + b) * c) - foo;
|
|
|
|
expect(op.operate(2, 3) == ((2 + 3) * 2) - foo);
|
2015-05-04 02:57:38 +00:00
|
|
|
}
|
|
|
|
}
|