回调函数function的test
生活随笔
收集整理的這篇文章主要介紹了
回调函数function的test
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
/*** Created by zengxc on 2018/2/22.* 回調函數在java中的應用*/ public interface Function<E, T> {public T callback(E e); } /*** Created by zengxc on 2018/2/22.*/ public class FunctionMethod {public void init(){System.out.println("start init");}public String run(String key, String value){System.out.println("runtime program");return key + value;}public void close(){System.out.println("end close");} } /*** Created by zengxc on 2018/2/22.*/ public class RedisUtils implements RedisUtilsImpl{private FunctionMethod functionMethod = new FunctionMethod();/*** 執行方法*/public <T> T execute(Function<FunctionMethod, T> function){FunctionMethod functionMethod = null;this.functionMethod.init();try {functionMethod = getNewClass();return function.callback(functionMethod);} catch (Exception e) {e.printStackTrace();} finally {if (null != functionMethod){// 關閉functionMethod.close();}}return null;}public FunctionMethod getNewClass(){return functionMethod;}@Overridepublic String set(final String key,final String value) {return this.execute(new Function<FunctionMethod, String>() {@Overridepublic String callback(FunctionMethod functionMethod) {return functionMethod.run(key, value);}});} @Test public void testFunction() {RedisUtils redisUtils = new RedisUtils();String set = redisUtils.set("call", "back");System.out.println(set); }console{
? ? start init
? ? runtime program
? ? end close
? ? callback
}
龍哥說懂得用回調是中級程序員與高級程序員的區別之一。
轉載于:https://my.oschina.net/u/3744350/blog/1622747
總結
以上是生活随笔為你收集整理的回调函数function的test的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CDS view注解解析 - @Envi
- 下一篇: react native的学习