OnJava8-Examples/innerclasses/LambdaExpressions.java

42 lines
922 B
Java
Raw Normal View History

2015-05-05 11:20:13 -07:00
//: innerclasses/LambdaExpressions.java
2015-05-06 12:09:38 -07:00
import static net.mindview.util.Print.*;
2015-05-05 11:20:13 -07:00
interface Description {
String brief();
}
2015-05-06 12:09:38 -07:00
interface Body {
String detailed(String head);
}
interface Multi {
String twoArg(String head, Double d);
}
2015-05-05 11:20:13 -07:00
public class LambdaExpressions {
Description desc = new Description() {
@Override
2015-05-06 12:09:38 -07:00
public String brief() {
return "Short info";
2015-05-05 11:20:13 -07:00
}
};
2015-05-06 12:09:38 -07:00
Description desc2 = () -> "Short info";
Body bod = (h) -> h + " More details";
Body bod2 = h -> h + " No Parens!";
Multi mult = (h, n) -> h + n;
// Parens are required with multiple args:
// Multi mult2 = h, n -> h + n; // Nope
public static void main(String[] args) {
LambdaExpressions le =
new LambdaExpressions();
print(le.desc.brief());
print(le.desc2.brief());
print(le.bod.detailed("Hi!"));
print(le.bod2.detailed("Oh!"));
print(le.mult.twoArg("Pi! ", 3.14159));
}
2015-05-05 11:20:13 -07:00
} ///:~