欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于接口实现java动态代理示例

程序员文章站 2024-02-26 12:32:16
subject.java复制代码 代码如下:package _20140416_; import java.util.list; public interface su...

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("中国"));
      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("中国").hashcode());
      system.out.println(new realsubject("中国").equals(new realsubject("中dd国")));
      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);
 }
}