Skip to content

Commit d522c79

Browse files
committed
change class name. add doc
1 parent 416e5bf commit d522c79

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

src/main/java/com/winterbe/java8/samples/lambda/Interface1.java

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.winterbe.java8.samples.lambda;
2+
3+
/**
4+
* @author Benjamin Winterberg
5+
*/
6+
public class InterfaceUsage {
7+
8+
public static void main(String[] args) {
9+
Formula formula1 = new Formula() {
10+
@Override
11+
public double calculate(int a) {
12+
return sqrt(a * 100);
13+
}
14+
};
15+
16+
double result1 = formula1.calculate(100); // 100.0
17+
double result2 = formula1.sqrt(-23); // 0.0
18+
int result3 = Formula.positive(-4); // 0.0
19+
20+
// Formula formula2 = (a) -> sqrt(a * 100);
21+
22+
System.out.println(result1);
23+
System.out.println(result2);
24+
System.out.println(result3);
25+
}
26+
}
27+
28+
/**
29+
* can define function in a interface class; function can be static.
30+
*/
31+
interface Formula {
32+
33+
double calculate(int a);
34+
35+
default double sqrt(int a) {
36+
return Math.sqrt(positive(a));
37+
}
38+
39+
static int positive(int a) {
40+
return a > 0 ? a : 0;
41+
}
42+
}

0 commit comments

Comments
 (0)