/** * java.lang.reflect.Proxy; 创建代理 * */ Starstar= (Star) Proxy.newProxyInstance( // 用于指定类加载器 去加载生成的代理类 ProxyUtil.class.getClassLoader(), // 指定接口 这些接口用于指定生成的代理长什么样 也就是有那些方法 // 能代理的方法都在接口中 newClass[]{Star.class}, // 指定生成的代理都要干啥 newInvocationHandler() { /** * * @param proxy the proxy instance that the method was invoked on * 代理的对象 * * @param method the {@code Method} instance corresponding to * the interface method invoked on the proxy instance. The declaring * class of the {@code Method} object will be the interface that * the method was declared in, which may be a superinterface of the * proxy interface that the proxy class inherits the method through. * 要运行的方法 例如: sing * * @param args an array of objects containing the values of the * arguments passed in the method invocation on the proxy instance, * or {@code null} if interface method takes no arguments. * Arguments of primitive types are wrapped in instances of the * appropriate primitive wrapper class, such as * {@code java.lang.Integer} or {@code java.lang.Boolean}. * 调用方法 传递的实参 * * @return * @throws Throwable */ @Override public Object invoke(Object proxy, Method method, Object[] args)throws Throwable { if("sing".equals(method.getName())){ System.out.println("准备话筒 收钱"); }elseif ("dance".equals(method.getName())){ System.out.println("准备场地 收钱"); }