例题一 :简单例题
package pkg;
import java.lang.reflect.*;
public class Test {
public static void main(String args[]) {
try {
Class c = Class.forName("java.lang.String");
Method[] m = c.getDeclaredMethods();
for (int i = 0; i < m.length; i++)
System.out.println(m[i].toString());
} catch (Throwable e) {
System.err.println(e);
}
}
}
例题二:找出类的方法
package pkg;
import java.lang.reflect.*;
public class Test {
private int methodDemo(Object p, int x) throws NullPointerException {
if (p == null)
throw new NullPointerException();
return x;
}
public static void main(String args[]) {
try {
Class cls = Class.forName("pkg.Test");
Method[] methlist = cls.getDeclaredMethods();
for (int i = 0; i < methlist.length; i++) {
Method m = methlist[i];
System.out.println("name = " + m.getName());
System.out.println("decl class = " + m.getDeclaringClass());
Class[] pvec = m.getParameterTypes();
for (int j = 0; j < pvec.length; j++) {
System.out.println("param #" + j + " " + pvec[j]);
}
Class[] evec = m.getExceptionTypes();
for (int j = 0; j < evec.length; j++) {
System.out.println("exc #" + j + " " + evec[j]);
}
System.out.println("return type = " + m.getReturnType());
System.out.println("-----");
}
} catch (Throwable e) {
System.err.println(e);
}
}
}
例题三:获取构造器信息
package pkg;
import java.lang.reflect.*;
public class Test {
public Test() {
}
protected Test(int i, double d) {
}
public static void main(String args[]) {
try {
Class cls = Class.forName("pkg.Test");
Constructor[] ctorlist = cls.getDeclaredConstructors();
for (int i = 0; i < ctorlist.length; i++) {
Constructor ct = ctorlist[i];
System.out.println("name = " + ct.getName());
System.out.println("decl class = " + ct.getDeclaringClass());
Class[] pvec = ct.getParameterTypes();
for (int j = 0; j < pvec.length; j++) {
System.out.println("param #" + j + " " + pvec[j]);
}
Class[] evec = ct.getExceptionTypes();
for (int j = 0; j < evec.length; j++) {
System.out.println("exc #" + j + " " + evec[j]);
}
System.out.println("-----------------------------------------------------");
}
} catch (Throwable e) {
System.err.println(e);
}
}
}
例题四:获取类的字段(域)
package pkg;
import java.lang.reflect.*;
public class Test {
private double d;
public static final int i = 37;
String s = "testing";
public static void main(String args[]) {
try {
Class cls = Class.forName("pkg.Test");
Field[] fieldlist = cls.getDeclaredFields();//cls.getFields()
for (int i = 0; i < fieldlist.length; i++) {
Field fld = fieldlist[i];
System.out.println("name = " + fld.getName());
System.out.println("decl class = " + fld.getDeclaringClass());
System.out.println("type = " + fld.getType());
int mod = fld.getModifiers();
System.out.println("modifiers = " + Modifier.toString(mod));
System.out.println("-----------------------------------------------------");
}
} catch (Throwable e) {
System.err.println(e);
}
}
}
例题五:根据方法的名称来执行方法
package pkg;
import java.lang.reflect.*;
public class Test {
public int add(int a, int b) {
return a + b;
}
public static void main(String args[]) {
try {
Class cls = Class.forName("pkg.Test");
Class[] partypes = new Class[2];
partypes[0] = Integer.TYPE;
partypes[1] = Integer.TYPE;
Method meth = cls.getMethod("add", partypes);
//Test methobj = new Test();
Object methobj = cls.newInstance();
Object[] arglist = new Object[2];
arglist[0] = new Integer(37);
arglist[1] = new Integer(47);
Object retobj = meth.invoke(methobj, arglist);
Integer retval = (Integer) retobj;
System.out.println(retval.intValue());
} catch (Throwable e) {
System.err.println(e);
}
}
}
例题六:创建新的对象
package pkg;
import java.lang.reflect.*;
public class Test {
public Test() {
}
public Test(int a, int b) {
System.out.println("a = " + a + " b = " + b);
}
public static void main(String args[]) {
try {
Class cls = Class.forName("pkg.Test");
Class[] partypes = new Class[2];
partypes[0] = Integer.TYPE;
partypes[1] = Integer.TYPE;
Constructor ct = cls.getConstructor(partypes);
Object[] arglist = new Object[2];
arglist[0] = new Integer(37);
arglist[1] = new Integer(47);
Object retobj = ct.newInstance(arglist);
} catch (Throwable e) {
System.err.println(e);
}
}
}
例题七:.改变字段(域)的值
package pkg;
import java.lang.reflect.*;
public class Test {
public double d;
public static void main(String args[]) {
try {
Class cls = Class.forName("pkg.Test");
Field fld = cls.getField("d");
Test test = new Test();
System.out.println("d = " + test.d);
fld.setDouble(test, 12.34);
System.out.println("d = " + test.d);
} catch (Throwable e) {
System.err.println(e);
}
}
}
例题八:数组
package pkg;
import java.lang.reflect.*;
public class Test {
public static void main(String args[]) {
try {
Class cls = Class.forName("java.lang.String");
Object arr = Array.newInstance(cls, 10);
Array.set(arr, 5, "this is a test");
String s = (String) Array.get(arr, 5);
System.out.println(s);
} catch (Throwable e) {
System.err.println(e);
}
}
}
例题九:5 x 10 x 15 的整型数组
package pkg;
import java.lang.reflect.*;
public class Test {
public static void main(String args[]) {
int[] dims = new int[] { 5, 10, 15 };
Object arr = Array.newInstance(Integer.TYPE, dims);
Object arrobj = Array.get(arr, 3);
Class cls = arrobj.getClass().getComponentType();
System.out.println(cls);
arrobj = Array.get(arrobj, 5);
Array.setInt(arrobj, 10, 37);
int arrcast[][][] = (int[][][]) arr;
System.out.println(arrcast[3][5][10]);
}
}
柳德才
13691193654
18942949207
QQ:422157370
liudecai_zan@126.com湖北-武汉-江夏-庙山