[Java提供了Class类,可以通过编程方式获取类别的字段和方法,包括构造方法 获取Class类实例的方法: 类名.class 实例名.getClass() Class.forName(className)public
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class AsClass {
public X x;
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) throws InstantiationException,
ClassNotFoundException, SecurityException, NoSuchMethodException,
IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
AsClass ac = new AsClass();
ac.x = new X();
String acName = ac.x.getClass().getSimpleName();
Class fake_x = Class.forName(acName);
Class[] newClass = new Class[1];
newClass[0] = String.class;
Method x_fun = fake_x.getMethod("show", newClass);
Object[] obj = new Object[1];
obj[0] = "Hello World";
x_fun.invoke(fake_x.newInstance(), obj);
}
}
class X {
public void show(String str) {
System.out.println(str);
}
}
[基本步骤和在java中调用C语言方法一样,只列出主要代码。MyTest.javapublic class MyTest{ private native String[] getInfo(); static { System.loadLibrary