关于作业的问题
关于作业的代码实现: InvocationHandler logh=new LogHandler(m); Class
cls2=m.getClass(); Moveable m2=(Moveable)Proxy.newProxyInstance(cls2.getClassLoader(),cls2.getInterfaces(),logh); m2.move(); 请问一下,以上实现方式有啥问题,为什么会报错?
关于作业的代码实现: InvocationHandler logh=new LogHandler(m); Class
cls2=m.getClass(); Moveable m2=(Moveable)Proxy.newProxyInstance(cls2.getClassLoader(),cls2.getInterfaces(),logh); m2.move(); 请问一下,以上实现方式有啥问题,为什么会报错?
2017-08-28
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", "true"); Subject subject = (Subject) Proxy.newProxyInstance(Client.class.getClassLoader(),new Class[]{Subject.class},new JdkProxySubject(new RealSubject())); subject.hello(); MathSubject mathSubject = new MathSubject(); InvocationHandler mathHandler= new ProxySubject(mathSubject); Class<?> cls = mathSubject.getClass(); Subject mathSubjectResult = (Subject) Proxy.newProxyInstance(Client.class.getClassLoader(), cls.getInterfaces(), mathHandler); InvocationHandler logmathHandler= new LogProxySubject(mathSubjectResult); Subject mathResult = (Subject) Proxy.newProxyInstance(Client.class.getClassLoader(), cls.getInterfaces(), logmathHandler); mathResult.hello("wahahaha"); mathResult.request();// 根据资料,添加了LogProxySubject, ProxySubjec , 替换了JdkProxySubject中的Class换为Interface 接口类Subject,这样就好了。
举报