File tree Expand file tree Collapse file tree 2 files changed +42
-35
lines changed
src/main/java/com/winterbe/java8/samples/lambda Expand file tree Collapse file tree 2 files changed +42
-35
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments