用URLClassLoader无法实现重新加载类,帮帮忙

04-06-27 cchess
public class A {

static{

System.out.println("开始初始化A");

}

public void print() {

System.out.println("Using Class A");

}

}

在一个程序的main方法中有段代码:

URL u = new URL("file:/C:/WINDOWS/jbproject/jdom/classes/");

URLClassLoader ucl = new URLClassLoader(new

URL[] {u});

Class c = ucl.loadClass("Word");

A asm = (A) c.newInstance();

asm.print();

System.out.println("=========================");

URL u1 = new URL("file:/C:/WINDOWS/jbproject/jdom/classes/");

URLClassLoader ucl1 = new URLClassLoader(new

URL[] {u1});

Class c1 = ucl1.loadClass("Word");

A asm1 = (A) c1.newInstance();

asm1.print();

运行结果为:

开始初始化A

Using Class A

=========================

Using Class A

并不是:

开始初始化A

Using Class A

=========================

开始初始化A

Using Class A

我是仿王森的《java深度历险》写,想实现两次重载Class A

fc.w
2004-06-27 23:32
顶,学习中

猜你喜欢