基于接口實(shí)現(xiàn)java動(dòng)態(tài)代理示例
Subject.java
package _20140416_;
import java.util.List;
public interface Subject {
public String say(String name,int age);
public List<Person> getAllList(String name);
}
RealSubject.java
package _20140416_;
import java.util.ArrayList;
import java.util.List;
public class RealSubject implements Subject {
private String name;
public RealSubject(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String say(String name, int age) {
return "名字:" + name + "_年齡:" + age;
}
@Override
public List<Person> getAllList(String name) {
List<Person> list = new ArrayList<Person>();
list.add(new Person("A", 20));
list.add(new Person("B", 20));
list.add(new Person("C", 20));
list.add(new Person("D", 20));
System.out.println(name);
return list;
}
@Override
public int hashCode() {
return 10010;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof RealSubject){
RealSubject real = (RealSubject)obj;
System.out.println("getName():"+real.getName());
System.out.println("this.name:"+this.name);
if(real.getName()==this.name){
System.out.println("相同");
}else{
System.out.println("不相同");
}
}
return true;
}
}
MyInvercationHander.java
package _20140416_;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class MyInvercationHander implements InvocationHandler{
private Object obj;
public Object bind(Object obj){
this.obj=obj;
return Proxy.newProxyInstance(obj.getClass().getClassLoader(),obj.getClass().getInterfaces(),this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object temp = method.invoke(this.obj, args);
//這里做切面操作 比如我要加功能
System.out.println("方法之前!");
return temp;
}
}
Person.java
package _20140416_;
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
MainTest.java
package _20140416_;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class MainTest {
public static void main(String[] args) {
Subject subject = (Subject) new MyInvercationHander().bind(new RealSubject("中國(guó)"));
System.out.println(subject.say("莫建鋒",22));
System.out.println(subject.getAllList("張三"));
Map<String,Integer> myMap = new HashMap<String, Integer>();
myMap.put("A",1);
myMap.put("B",2);
myMap.put("C",3);
myMap.put("D",4);
myMap.put("E",5);
Set<Map.Entry<String,Integer>> myEntrySet = myMap.entrySet();
Iterator<Map.Entry<String,Integer>> it = myEntrySet.iterator();
while(it.hasNext()){
Map.Entry<String,Integer> entry = it.next();
System.out.print(entry.getKey()+":");
System.out.println(entry.getValue());
}
System.out.println(new RealSubject("中國(guó)").hashCode());
System.out.println(new RealSubject("中國(guó)").equals(new RealSubject("中dd國(guó)")));
String info = new String("1");
String info1 = new String("1");
System.out.println(new Integer('1'));
System.out.println(info.hashCode());
System.out.println(info1.hashCode());
System.out.println(info==info1);
System.out.println(info.equals(info1));
short i = 1;
System.out.println(i);
}
}
相關(guān)文章
Java利用序列化實(shí)現(xiàn)對(duì)象深度clone的方法
這篇文章主要介紹了Java利用序列化實(shí)現(xiàn)對(duì)象深度clone的方法,實(shí)例分析了java序列化及對(duì)象克隆的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07SpringBoot項(xiàng)目在啟動(dòng)后自動(dòng)關(guān)閉的實(shí)現(xiàn)
我們?cè)趯憇pring?boot?web項(xiàng)目時(shí),有時(shí)會(huì)遇到啟動(dòng)后立即關(guān)閉的情況,?本文主要介紹了SpringBoot項(xiàng)目在啟動(dòng)后自動(dòng)關(guān)閉的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01Java?SM2加密相關(guān)實(shí)現(xiàn)與簡(jiǎn)單原理詳解
SM2算法可以用較少的計(jì)算能力提供比RSA算法更高的安全強(qiáng)度,而所需的密鑰長(zhǎng)度卻遠(yuǎn)比RSA算法低,這篇文章主要給大家介紹了關(guān)于Java?SM2加密相關(guān)實(shí)現(xiàn)與簡(jiǎn)單原理的相關(guān)資料,需要的朋友可以參考下2024-01-01Mybatis-plus的selectPage()分頁(yè)查詢不生效問(wèn)題解決
本文主要介紹了Mybatis-plus的selectPage()分頁(yè)查詢不生效問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01Springboot單元測(cè)試無(wú)法讀取配置文件的解決方案
這篇文章主要介紹了Springboot單元測(cè)試無(wú)法讀取配置文件的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java?Servlet響應(yīng)httpServletResponse過(guò)程詳解
HttpServletResponse是處理http響應(yīng)的對(duì)象,調(diào)用該對(duì)象的方法,設(shè)置到對(duì)象屬性的內(nèi)容,tomcat最終會(huì)組織為http響應(yīng)報(bào)文2022-02-02