黑马程序员技术交流社区
标题: 用java读取Properties文件来改变实现类 [打印本页]
作者: 张强+ 时间: 2011-10-21 22:10
标题: 用java读取Properties文件来改变实现类
导读:Test1.java: package test; import java.io.*;import java.lang.reflect.*; class AClassLoader extends ClassLoader { public Class loadClass(String name, boolean resolve) throws ClassNotFoundException { Class klass = null; try { klass = findLoadedClass(name); //检查该类是否已经被装载。 if (klass != null) { return klass; } byte[] bs = getClassBytes(name);//从一个特定的信息源寻找并读取该类的字节。 if (bs != null && bs.length > 0) { klass = defineClass(name, bs, 0, bs.length); } if (klass == null) { //如果读取字节失败,则试图从JDK的系统API中寻找该类。 klass = findSystemClass(name); } if (resolve && klass != null) { resolveClass(klass); } } catch (IOException e) { throw new ClassNotFoundException(e.toString()); } System.out.println("klass == " + klass); return klass; } private byte[] getClassBytes(String className) throws IOException { String path = System.getProperty("java.class.path") + File.separator; path += className.replace(., File.separatorChar) + ".class"; System.out.println(path); FileInputStream fis = null; try { fis = new FileInputStream(path); } catch (FileNotFoundException e) { System.out.println(e); return null; //如果查找失败,则放弃查找。捕捉这个异常主要是为了过滤JDK的系统API。 } byte[] bs = new byte[fis.available()]; fis.read(bs); return bs; }} public class Test1 { public static void main(String[] args) throws Exception{ AClassLoader loader = new AClassLoader(); Class c = loader.loadClass("test.A", false); System.out.println(c); Object o = c.newInstance(); System.out.println(o); Method m = c.getMethod("print", java.lang.String.class); m.invoke(o, "bitan"); } } A.java package test;public class A { public void print(String str) { System.out.println("嗨," + str + ", 你终于找到我了!"); } public String toString() { return "我是一个A对象!"; } } 结果: C:
作者: 祁焱 时间: 2011-10-21 22:41
package test;
import java.io.*;
import java.lang.reflect.*;
class AClassLoader extends ClassLoader {
public Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
Class klass = null;
try {
klass = findLoadedClass(name); //检查该类是否已经被装载。
if (klass != null) {
return klass;
}
byte[] bs = getClassBytes(name);//从一个特定的信息源寻找并读取该类的字节。
if (bs != null && bs.length > 0) {
klass = defineClass(name, bs, 0, bs.length);
}
if (klass == null) { //如果读取字节失败,则试图从JDK的系统API中寻找该类。
klass = findSystemClass(name);
}
if (resolve && klass != null) {
resolveClass(klass);
}
} catch (IOException e) {
throw new ClassNotFoundException(e.toString());
}
System.out.println("klass == " + klass);
return klass;
}
private byte[] getClassBytes(String className) throws IOException {
String path = System.getProperty("java.class.path") + File.separator;
path += className.replace(.
, File.separatorChar)
+ ".class";
System.out.println(path);
FileInputStream fis = null;
try {
fis = new FileInputStream(path);
} catch (FileNotFoundException e) {
System.out.println(e);
return null; //如果查找失败,则放弃查找。捕捉这个异常主要是为了过滤JDK的系统API。
}
byte[] bs = new byte[fis.available()];
fis.read(bs);
return bs;
}
}
public class Test1 {
public static void main(String[] args) throws Exception {
AClassLoader loader = new AClassLoader();
Class c = loader.loadClass("test.A", false);
System.out.println(c);
Object o = c.newInstance();
System.out.println(o);
Method m = c.getMethod("print", java.lang.String.class);
m.invoke(o, "bitan");
}
}
A.java package test;
public class A {
public void print(String str) {
System.out.println("嗨," + str + ", 你终于找到我了!");
}
public String toString() {
return "我是一个A对象!";
}
}
结果:
C
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |