|
| 1 | +import java.util.function.Function; |
| 2 | + |
| 3 | +/** |
| 4 | + * In this Challenge, We learn: |
| 5 | + * * String.join() method: String message = String.join("-", "Java", "is", "cool"); |
| 6 | + * message = "Java-is-cool"; |
| 7 | + * * Function<String, String> function ----> function.apply() : given an argument of type String the function |
| 8 | + * return a String. |
| 9 | + * * the overridden method in a anonymous class could be called only if we reference the object: |
| 10 | + * new Jedi(){ |
| 11 | + * overriddenMethod(){...} |
| 12 | + * }; |
| 13 | + * the above method will be never used, unless we write |
| 14 | + * new Jedi(){ |
| 15 | + * overriddenMethod(){...} |
| 16 | + * }.overriddenMethod(); |
| 17 | + * |
| 18 | + */ |
| 19 | +public class Challenge_2 { |
| 20 | + |
| 21 | + interface Jedi{ |
| 22 | + String MASTER = "Yoda"; |
| 23 | + |
| 24 | + //default String attack(){return jump(jedi-> String.join(jedi, useSaber(), useForce())); } |
| 25 | + default String attack(){ |
| 26 | + return jump(stringInput-> String.join(stringInput, useSaber(), this.useForce())); |
| 27 | + } |
| 28 | + |
| 29 | + private String jump( Function<String, String> function){ |
| 30 | + return function.apply("Luke"); |
| 31 | + } |
| 32 | + |
| 33 | + private static String useSaber(){return "S";} |
| 34 | + |
| 35 | + private String useForce(){return "F";} |
| 36 | + } |
| 37 | + |
| 38 | + public static void main( String[] args ) { |
| 39 | + System.out.println(new Jedi(){ |
| 40 | + public String useForce(){return "X";}}.attack()+ Jedi.useSaber() + Jedi.MASTER); |
| 41 | + } |
| 42 | +} |
0 commit comments