1,創(chuàng)建一個(gè)UserService類(lèi):
public interface UserService { public String getTheName(int id); public Integer getTheAge(int id); }
2,創(chuàng)建實(shí)現(xiàn)類(lèi)UserServiceImpl
public class UserServiceImpl implements UserService { @Override public String getTheName(int id) { return "小風(fēng)"; } @Override public Integer getTheAge(int id) { return 10; } }
3, 測(cè)試類(lèi)JDKProxyTest
public class JDKProxyTest implements InvocationHandler { private Object target; JDKProxyTest() { super(); } JDKProxyTest(Object target) { super(); this.target = target; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("++++++調(diào)用方法之前: " + method.getName() + "++++++"); Object result = method.invoke(target, args); System.out.println("++++++調(diào)用方法之后: " + method.getName() + "++++++"+result); return result; } public static void main(String[] args) { UserServiceImpl userService = new UserServiceImpl(); InvocationHandler handler=new JDKProxyTest(userService); UserService userServiceProxy= (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), handler); System.out.println(userServiceProxy.getTheName(1)); System.out.println(userServiceProxy.getTheAge(1)); } }
當(dāng)前名稱(chēng):JDK動(dòng)態(tài)代理demo-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://www.chinadenli.net/article30/dpdjpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站收錄、企業(yè)建站、網(wǎng)站改版、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容