本篇內容主要講解“java動態(tài)代理的簡單使用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“java動態(tài)代理的簡單使用”吧!

//---------------接口
package reflect_test;
public interface Animals {
public String scrime(String str);
}
//
--------------類
package reflect_test;
public class Dog implements Animals {
private int age =100;
private String xstr="test...";
public int page = 100000;
private Dog(String xstr) {
this.xstr = xstr;
}
public Dog() {
}
public Dog(int age) {
this.age = age;
}
@Override
public String scrime(String str) {
return str;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private int getAddAge() {
return age+=10;
}
public void getXstr() {
System.out.println(xstr);
}
}
-------------代理類
package reflect_test;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.junit.Before;
import org.junit.Test;
public class ProxyTest {
@Test
public void testProxy() throws Exception {
Class<?> clazz = Class.forName("reflect_test.Dog");
Class<?>[] interfa = {Class.forName("reflect_test.Animals")};
Animals am = (Animals)Proxy.newProxyInstance(clazz.getClassLoader(), interfa, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String str = (String)method.invoke(new Dog(), args);
return str + "\n append by proxy !";
}
});
System.out.println(am.scrime("normal scriming....."));
}
}到此,相信大家對“java動態(tài)代理的簡單使用”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
文章名稱:java動態(tài)代理的簡單使用-創(chuàng)新互聯
分享網址:http://chinadenli.net/article40/hgjeo.html
成都網站建設公司_創(chuàng)新互聯,為您提供關鍵詞優(yōu)化、靜態(tài)網站、ChatGPT、品牌網站制作、營銷型網站建設、商城網站
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯