代码
public interface RequestHandler { void onRequest(String recive, String toRespon); }public interface RequestHandler { void onRequest(String recive, String toRespon); }
public class RpcServer { private RequestHandler handler = new RequestHandler() { @Override public void onRequest(String recive, String toResp) { System.out.println(recive); System.out.println(toResp); } }; public RequestHandler getHandler() { return handler; } public void setHandler(RequestHandler handler) { this.handler = handler; } }
public static void main( String[] args ) { System.out.println( "Hello World!" ); //stringAl1(); // StringAlgorithm1.test(); RpcServer rpcServer = new RpcServer(); RequestHandler handler = rpcServer.getHandler(); handler.onRequest("hhei","ouou"); }
使用的原因:只需要临时需要创建一个接口的实现类,上面的"技巧"可以用来简化代码
https://cloud.tencent.com/developer/article/1027960?from=15425