Skip to content

Commit 82a97e1

Browse files
committed
class 19 default methods in interfaces
1 parent dce25e9 commit 82a97e1

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

modules/src/main/java/com/platzi/functional_student_practice/_08_lambda/Syntax.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.platzi.functional_teacher_theory._06_reference_operator.NombresUtils;
44

5+
import java.util.Arrays;
56
import java.util.List;
67
import java.util.function.BiFunction;
78
import java.util.function.Predicate;
@@ -10,7 +11,7 @@ public class Syntax {
1011
public static void main(String[] args) {
1112

1213
System.out.println("////////////////////////////////////////////////////////////////////////////////////");
13-
System.out.println("\nCLASS 17\n");
14+
System.out.println("\nCLASS 18\n");
1415

1516
// 1 input, no return
1617
List<String> cursos = NombresUtils.getList("Java", "Functional");
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.platzi.functional_student_practice._09_defaults;
2+
3+
public class StringFunctions {
4+
public static void main(String[] args) {
5+
System.out.println("////////////////////////////////////////////////////////////////////////////////////");
6+
System.out.println("\nCLASS 19\n");
7+
8+
StringOperation six = () -> 6;
9+
// 1b. Execution of default method
10+
six.operate("Alumno");
11+
// 2b. Execution of concatenated default methods
12+
six.reduceAmount(2).operate("Profesor");
13+
14+
// 3. Execution of .functionSaved() method
15+
DoOperation operateFive = text -> System.out.println(text);
16+
// 4. Execution of saved function 5 times with "Platzi" String
17+
operateFive.execute(5, "Platzi");
18+
}
19+
20+
@FunctionalInterface
21+
interface StringOperation {
22+
int getAmount();
23+
24+
// 1. Default method. Functional Interfaces require 1 and only 1 abstract method, but you can add any amount of default methods
25+
default void operate(String text) {
26+
int x = getAmount();
27+
while (x-- > 0) {
28+
System.out.println(text);
29+
}
30+
}
31+
32+
// 2. Extra default method, wich return another StringOperation Object that can be called operate with a modificed Amount
33+
default StringOperation reduceAmount(int x) {
34+
int old = getAmount();
35+
return () -> old - x;
36+
}
37+
}
38+
39+
@FunctionalInterface
40+
interface DoOperation {
41+
void functionSaved(String text);
42+
43+
default void execute(int x, String text) {
44+
while (x-- > 0) {
45+
functionSaved(text);
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)