问题:关于动态代理。 jdk1.3引入的动态代理号称可以在运行时实现多个接口。但我发觉这是假的,譬如我有一个类,Donkey(驴子),我现在要把它变成飞马,当然是在运行时。那么我想动态的让他实现CanRun接口和CanFly接口,结果做不到,除非Donkey类编译时就声明implements CanFly, CanRun。是这样吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 写个例子import java.lang.reflect.*;import java.util.*;interface CanFly{ void fly(String name, int value);}interface CanRun{ void run(String name, int value);}public class ProxyTest{ public static void testCanFly(CanFly fly) { fly.fly("hello", 3); } public static void testCanRun(CanRun run) { run.run("world", 5); } public static void main(String[] args) { InvocationHandler h = new Donkey(); Object proxy = Proxy.newProxyInstance(CanFly.class.getClassLoader(), new Class[]{ CanFly.class, CanRun.class }, h); testCanFly((CanFly)proxy); testCanRun((CanRun)proxy); }}class Donkey implements InvocationHandler{ public Object invoke(Object proxy, Method m, Object[] args) { System.out.print("Method name: " + m.getName() + "("); if (args != null) { for (int i = 0; i < args.length; i++) { System.out.print(args[i]); if (i < args.length - 1) { System.out.print(", "); } } } System.out.println(")"); return null; }}; 没有问题呀,JDK代理只代理接口什么叫实现接口:就是implements 了某个接口而不是说你写了一个和接口同名的方法就实现了接口 矛盾了!帮忙看下! Too many open files 大家看看这个问题怎么解决 我想在swing写的框架里面,内置一个浏览器,不知道如何实现,高分求问 如何在eclipse开发工具下用出这个东西? 什么是assert? help:一段简单的JAVA程序运行出错 急! 参与者均有分相送 . Java能否用Jni调用使用MFC的DLL? html页面上的透明传输 String逐位得到字符的方法? 有关finalize 如何取得表示上月的格式化字符串?
import java.util.*;interface CanFly
{
void fly(String name, int value);
}interface CanRun
{
void run(String name, int value);
}public class ProxyTest
{
public static void testCanFly(CanFly fly)
{
fly.fly("hello", 3);
} public static void testCanRun(CanRun run)
{
run.run("world", 5);
} public static void main(String[] args)
{
InvocationHandler h = new Donkey();
Object proxy = Proxy.newProxyInstance(CanFly.class.getClassLoader(),
new Class[]{ CanFly.class, CanRun.class }, h);
testCanFly((CanFly)proxy);
testCanRun((CanRun)proxy);
}
}class Donkey implements InvocationHandler
{
public Object invoke(Object proxy, Method m, Object[] args)
{
System.out.print("Method name: " + m.getName() + "("); if (args != null)
{
for (int i = 0; i < args.length; i++)
{
System.out.print(args[i]);
if (i < args.length - 1)
{
System.out.print(", ");
}
}
} System.out.println(")"); return null;
}
};
什么叫实现接口:就是implements 了某个接口
而不是说你写了一个和接口同名的方法就实现了接口