// 一般采用这种形式(需要处理异常 ClassNotFoundException )
Class> class1 = Class.forName("com.yztc.lin.model.TestReflect");
Class> class2 = new TestReflect().getClass();
Class> class3 = TestReflect.class;
System.out.println("类名称 " + class1.getName());// getName 获得完整的类名(含包名)
System.out.println("类名称 " + class2.getName());
System.out.println("类名称 " + class3.getName());
// 取得父类
Class> parentClass = class1.getSuperclass();
System.out.println("clazz的父类为:" + parentClass.getName());
// 获取所有的接口
Class> intes[] = clazz.getInterfaces();
System.out.println("clazz实现的接口有:");
for (int i = 0; i < intes.length; i++) {
System.out.println((i + 1) + ":" + intes[i].getName());
}
Constructor> con = class1.getConstructor(int.class, String.class);//传递具体的参数列表的Class
Class> clazzs[] = con.getParameterTypes();// 获得该构造器的参数列表
System.out.print("con :");
for (int i = 0; i < clazzs.length; i++)
System.out.print(clazzs[i].getName() + "\t");
Constructor> cons[] = class1.getConstructors();
// 查看每个构造方法需要的参数
for (int i = 0; i < cons.length; i++) {
System.out.print("cons[" + i + "] :");
Class> clazzs[] = cons[i].getParameterTypes();//获得该构造器的参数列表
for (int j = 0; j < clazzs.length; j++)
System.out.print(clazzs[j].getName() + "\t");
System.out.println();
}
2.3 通过反射机制实例化一个类的对象
Class> class1 = Class.forName("com.yztc.lin.model.Student");
Student stu = (Student) class1.newInstance();//默认走空构造器
stu.setAge(20);
stu.setName("Rollen");
System.out.println(user.toString());
Class> class1 = Class.forName("com.yztc.lin.model.Student");
//Constructor> cons[] = class1.getConstructors();
//Constructor> con =cons[1];
Constructor> con = class1.getConstructor(int.class, String.class);//传递具体的参数列表的Class
Student stu = (Student) con.newInstance(18,"张三");//走两个参数的空构造器
System.out.println(user.toString());
// 取得本类的 age 属性
Field f = clazz.getDeclaredField("age");
//修饰符类型
int mo = f.getModifiers();
String priv = Modifier.toString(mo);
// 属性类型
Class> type = f.getType();
System.out.println(priv + " " + type.getName() + " " + f.getName() + ";");
// 取得本类的全部属性
Field[] fields = clazz.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
String s = Modifier.toString(f.getModifiers())+" "+f.getType().getName()+" "+f.getName();
System.out.println(s);
}
// 取得接口或父类的 age 属性
Field f = clazz.getField("name");
String s = Modifier.toString(f.getModifiers())+" "+f.getType().getName()+" "+f.getName();
System.out.println(s);
// 取得本类的全部属性
Field[] fields = clazz.getFields();
for (int i = 0; i < fields.length; i++) {
Field f = fields[i];
String s = Modifier.toString(f.getModifiers())+" "+f.getType().getName()+" "+f.getName();
System.out.println(s);
}
Class> clazz = Student.class;
Object obj = clazz.newInstance();
Field field = clazz.getDeclaredField("name");
//field.setAccessible(true);//可以直接对 private 的属性赋值
field.set(obj, "Java反射修改属性值");
System.out.println(field.get(obj));//获取属性的值
3. Class> getReturnType() 获取返回值类型
4.String getName() 获取方法名
5.Class>[] getParameterTypes() 获取参数列表6. invoke 调用方法
Method method = class1.getDeclaredMethod("privateMethod", String.class, int.class);
int modifiers = method.getModifiers();//得到修饰符类型 返回int值
String modifierStr = Modifier.toString(modifiers);//根据修饰符类型 返回具体的修饰符字符串
Class> returnType = method.getReturnType();//得到返回值类型
String methodName = method.getName();//得到方法名
System.out.print(modifierStr+" "+returnType+" "+methodName+" (");
Class>[] parameterTypes = method.getParameterTypes();// 得到方法的参数列表是
for (int i = 0; i < parameterTypes.length; i++) {
Class> c = parameterTypes[i];
if (i==parameterTypes.length-1) {
System.out.print(c.getName()+" arg"+i);
break;
}
System.out.print(c.getName()+" arg"+i+",");
}
System.out.println(")");
Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
System.out.print(Modifier.toString(method.getModifiers()) + " " + method.getReturnType() + " " + method.getName() + " (");
Class>[] parameterTypes = method.getParameterTypes();// 得到方法的参数列表是
for (int i = 0; i < parameterTypes.length; i++) {
Class> c = parameterTypes[i];
if (i == parameterTypes.length - 1) {
System.out.print(c.getName() + " arg" + i );
break;
}
System.out.print(c.getName() + " arg" + i + ",");
}
System.out.println(")");
}
Method method = class1.getMethod("publicMethod", String.class, int.class);
System.out.print(Modifier.toString(method.getModifiers()) + " " + method.getReturnType() + " " +method.getName() + " (");
Class>[] parameterTypes = method.getParameterTypes();// 得到方法的参数列表是
for (int i = 0; i < parameterTypes.length; i++) {
Class> c = parameterTypes[i];
if (i == parameterTypes.length - 1) {
System.out.print(c.getName() + " arg" + i );
break;
}
System.out.print(c.getName() + " arg" + i + ",");
}
System.out.println(")");
Method[] methods = clazz.getMethods();
for (Method method : methods) {
System.out.print(Modifier.toString(method.getModifiers()) + " " + method.getReturnType() + " " + method.getName() + " (");
Class>[] parameterTypes = method.getParameterTypes();// 得到方法的参数列表是
for (int i = 0; i < parameterTypes.length; i++) {
Class> c = parameterTypes[i];
if (i == parameterTypes.length - 1) {
System.out.print(c.getName() + " arg" + i );
break;
}
System.out.print(c.getName() + " arg" + i + ",");
}
System.out.println(")");
}
public class Test {
public static void main(String[] args) throws Exception {
Class> clazz = Class.forName("com.yztc.lin.model.Test");
Object obj = clazz.newInstance();
/** 调用Test类中的 test1 方法**/
Method method = clazz.getDeclaredMethod("test1");
//method.setAccessible(true);//打破访问权限的范围限制
Object returnVal = method.invoke(obj);
System.out.println("得到返回值:"+returnVal);
/**调用Test的test1方法**/
method = clazz.getMethod("test2", int.class, String.class);
method.invoke(obj, 20, "张三");
}
private int test1() {
System.out.println("通过反射调用了方法1");
return 123;
}
public void test2(int i, String s) {
System.out.println("通过反射调用了方法2 i:" + i + " s:" + s);
}
}