在网上找了例子,自己改改,写写,注释注释。下面把我对functionalinterface里面的function的理解过程贴上来。
import java.util.Objects;
@FunctionalInterface
public interface FunctionC<T,R>{
/**
* function of interface FunctionC<T,R>
*/
R apply(T t);
/**
* using lambda to describe method compose
* andThen = (T t) -> after.apply(apply(t));
*
* default: no need to realized
*
* <V>: declare an Object V
*
* FunctionC<T,V>: declare an Interface FunctionC but using<T,V>,
* this means Interface FunctionC<T,V>{ V apply(T t);}
*
* andThen: method name
*
* FunctionC<? super R, ? extends V> after: declare the param type
* which is an realized object named 'after' that is of Interface FunctionC but using <? super R, ? extends V>,
* and that means Interface FunctionC<? super R, ? extends V>{ ? extends V apply(? super R r);}
* which almostly equals Interface FunctionC<R, V>{ V apply(R r);}
*
* by the way, the declaration of this interface is FunctionC<T,R>{ R apply(T t);}
* so we have declared three interface with type-relationship-table
*<table>
*<tr><th>InterfaceGenericType</th><th>ReturnType</th><th>ParamType</th><th>IsEntrance</th></tr>
*<tr><td>FunctionC<T,R></td><td>R</td><td>T</td><td>No</td></tr>
*<tr><td>FunctionC<T,V></td><td>V</td><td>T</td><td>Yes</td></tr>
*<tr><td>FunctionC<R,V></td><td>V</td><td>R</td><td>No</td></tr>
*</table>
*
* then if we want to get V by using T as param, we could use this type-relationship-table
* we send T to function apply(T) then we get R, and we sent R to function apply(R) we get V.
* so we get the V by using function ? apply(?), and the type-relationship-table.
* so we put all we need into the declaretion statement, down here.
*/
default <V> FunctionC<T,V> andThen(FunctionC <? super R, ? extends V> after){
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
/**
* using lambda to describe method compose
* compose = (V v) -> apply(before.apply(v))
*
* default: no need to realized
*
* <V>: declare an Object V
*
* FunctionC<V,R>: declare an Interface FunctionC but using<V,R>,
* this means Interface FunctionC<V,R>{ R apply(V v);}
*
* compose: method name
*
* FunctionC <? super V, ? extends T> before: declare the param type
* which is an realized object named 'before' that is of Interface FunctionC but using <? super V, ? extends T>,
* and that means Interface FunctionC<? super V, ? extends T>{ ? extends T apply(? super V v);}
* which almostly equals Interface FunctionC<V, T>{ T apply(V v);}
*
* by the way, the declaration of this interface is FunctionC<T,R>{ R apply(T t);}
* so we have declared three interface with type-relationship-table
*<table>
*<tr><th>InterfaceGenericType</th><th>ReturnType</th><th>ParamType</th><th>IsEntrance</th></tr>
*<tr><td>FunctionC<T,R></td><td>R</td><td>T</td><td>No</td></tr>
*<tr><td>FunctionC<V,R></td><td>R</td><td>V</td><td>Yes</td></tr>
*<tr><td>FunctionC<V,T></td><td>T</td><td>V</td><td>No</td></tr>
*</table>
*
* then if we want to get R by using V as param, we could use this type-relationship-table
* we send V to function apply(V) then we get T, and we sent T to function apply(T) we get R.
* so we get the R by using function ? apply(?), and the type-relationship-table.
* so we put all we need into the declaretion statement, down here.
*/
default <V> FunctionC<V,R> compose(FunctionC <? super V, ? extends T> before){
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
}
public class AndThenTest{
public static void main(String args[]){
FunctionC<Integer, Integer> func1 = num -> num+1;
FunctionC<Integer, Integer> func2 = num -> num*10;
FunctionC<Integer, Integer> func3 = num -> num-5;
final Integer i = 10;
System.out.println("========================function.andThen=========================");
System.out.println("while num is " + i );
System.out.println(", num+1 is: " + func1.apply(i));
System.out.println(", and (num+1).andThen(num*10).andThen(num-5) is: " +
func1.andThen(func2).andThen(func3).apply(i));
FunctionC<Integer, Integer> func4 = num -> num;
System.out.println("use lambda num.andThen(num -> num*10).andThen(num -> num-5) is: " +
func4.andThen(num -> num*10).andThen(num -> num-5).apply(i));
System.out.println("========================function.compose=========================");
System.out.println("while num is " + i );
System.out.println(", num+1 is: " + func1.apply(i));
System.out.println(", and (num+1).compose.(num*10).compose(num-5) is: " +
func1.compose(func2).compose(func3).apply(i));
}
}