谢谢,我也不太懂,就是学习,基本上我理解:简单讲是利用jdk提供的reflect包里面的类提供动态构造对象的功能,就是我告诉Proxy类我要执行什么方法,我给你一个对象,并且告诉他我这个对象都实现那些接口,然后他就帮我去构造一个对象,但是这个对象因为实现invocationHandle接口,就具有一种特性,就是凡是调用他的方法(换一种说法,就是外部向这个对象所发送的任何消息)都经过invoke()里面处理,这样就达到我的目的。我的目的就是接管被代理对象的方法
我遇到的问题是:Student s=(Student)Proxy.newProxyInstance(cl,i,this);执行失败没有构造到我想要的对象,是个null,弄了很久,无奈。        
我估计没说清楚,努力吧!

解决方案 »

  1.   

    动态代理技术是一种很重要的框架技术之一,在很多技术里面都有实现 ,csdn save me!
      

  2.   

    我也没有听说过
    看来Java 又加了不少东西~~~~~
    我光知道1.5(tiger)里加了一些什么泛型,枚举,听Borland 的首席技术专家讲的课间 
    也没有觉出来多大好处
    倒是以来了不少麻烦,专家都这么说的
    当然最后的版本还没有确定下来,希望能有所改进~~~~~~~~~~~
    不然,以后的Java还是我喜欢的Java 吗??
      

  3.   

    这儿Student必须是个interface,而不是一个具体的类。
      

  4.   

    import java.lang.reflect.*;
    interface Stu{
    public void eat();
    public void play();
    public void sleep();
    public void study();
    public void work();
    }
    class Student implements Stu{ public void eat() {
    }

    public void play() {
    System.out.println("student play 4 hours!");
    }

    public void sleep() {
    System.out.println("student sleep 8 hours!");
    }

    public void study() {
    System.out.println("student study 8 hours!");
    }

    public void work() {
    System.out.println("student work 0 hours!");
    }
    }/**_Student.java*/
    public class _Student implements InvocationHandler {
    static final String PLAY="play";
    static final String SLEEP="sleep";
    static final String EAT="eat";
    static final String WORK="work";

    private Student student;

    public _Student(Student student){
    this.student=student;
    }
    public static void main(String[] args) {
    _Student _s=new _Student(new Student());
    //System.out.println(_s.getStudent());
    Stu s=(Stu)_s.getStudent();
    s.eat();
    s.play();
    s.work();

    } public Object invoke(Object proxy, Method method, Object[] args)
    throws Throwable {
    Object result=null;
    if(PLAY.equals(method.getName())){
    System.out.println("_Student play 5 hours");
    }

    if(SLEEP.equals(method.getName())){
    System.out.println("_student sleep 9 hours!");
    }

    if(EAT.equals(method.getName())){
    result=method.invoke(student,args);
    }

    return result;
    }

    public Stu getStudent(){
    ClassLoader cl=student.getClass().getClassLoader();
    Class[] i=student.getClass().getInterfaces();
    Stu s=(Stu)Proxy.newProxyInstance(cl,i,this);
    return s;
    }

    }