问题:关于动态代理。 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 了某个接口而不是说你写了一个和接口同名的方法就实现了接口 SQLException如何知道造成的原因 java实现二叉树旋转距离计算 评价一下<java2 入门经典>这本书,我正想........ 请问为什么程序运行后什么控件也没有 怎么才能让jtextpane不自动换行 配置文件采用xml格式,配置信息采用层次结构,如何根据层次读出这些配置信息? jdbc连接sql server 2000,insert不能编译??? 这个程序不能运行,请指教 关于java序列化的问题请教。 写了一个JDBC往数据库增加信息,改了很久还是错误,求各位高手帮看看指导下错误 有关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 了某个接口
而不是说你写了一个和接口同名的方法就实现了接口