OnJava8-Examples/innerclasses/LambdaExpressions.java
2015-09-07 11:44:36 -06:00

50 lines
1.0 KiB
Java

// innerclasses/LambdaExpressions.java
// ©2015 MindView LLC: see Copyright.txt
import static com.mindviewinc.util.Print.*;
interface Description {
String brief();
}
interface Body {
String detailed(String head);
}
interface Multi {
String twoArg(String head, Double d);
}
public class LambdaExpressions {
Description desc = new Description() {
@Override
public String brief() {
return "Short info";
}
};
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));
}
}
/* Output:
Short info
Short info
Hi! More details
Oh! No Parens!
Pi! 3.14159
*/