JAVA之反射
2016-07-13 20:47
281 查看
访问成员变量
访问方法
或者
访问类的成员
import java.lang.reflect.Constructor; public class test{ public int i; protected boolean b; private double dd; }
import java.awt.print.Paper; import java.lang.reflect.Field;; public class fun{ public static void main(String[] args){ Field[] ff = test.class.getDeclaredFields(); test t = new test(); for(int i = 0;i<ff.length;i++){ Field field = ff[i]; System.out.println("名称为:" + field.getName()); Class fieldType = field.getType(); System.out.println("类型为:" + fieldType); boolean isTurn = true; while(isTurn){ try { isTurn = false; System.out.println("修改之前的值:" + field.get(t)); if(fieldType.equals(int.class)){ field.setInt(t, 168); }else{ if(fieldType.equals(boolean.class)){ field.setBoolean(t, true); }else{ if(fieldType.equals(double.class)){ field.setDouble(t, 1.11); } } } System.out.println("修改之后的值为:" + field.get(t)); } catch (Exception e) { System.out.println("异常出现..."); field.setAccessible(true); isTurn = true; } } } } }
访问方法
import java.lang.reflect.Constructor; public class test{ static void static_A(){ System.out.println("static_A"); } public int public_B(int i){ System.out.println("public_B"); return i*100; } protected int protected_C(String s,int i){ System.out.println("protected_C"); return Integer.valueOf(s) + i; } private String private_D(String... strings){ System.out.println("private_D"); StringBuffer sb = new StringBuffer(); for(int i=0;i<strings.length;i++){ sb.append(strings[i]); } return sb.toString(); } }
import java.lang.reflect.Method; public class fun{ public static void main(String[] args){ Method[] dm = test.class.getDeclaredMethods(); //System.out.println(dm.length); test t = new test(); for(int i=0;i<dm.length;i++){ Method method = dm[i]; System.out.println("方法名称为:" + method.getName()); Class[] paramentsType = method.getParameterTypes(); System.out.println("参数类型分别为:"); for(int j =0;j<paramentsType.length;j++){ System.out.println(paramentsType[j]); } Class returnType = method.getReturnType(); System.out.println("返回值类型为:" + returnType); //System.out.println(method.getModifiers());//static=8 public=1 protected=4 private=130 int modifiersNum = method.getModifiers(); boolean flag = true; while(flag){ try { flag = false; if(modifiersNum == 8){ method.invoke(t); }else{ if(modifiersNum == 1){ System.out.println("返回值为:" + method.invoke(t, 168)); }else{ if(modifiersNum == 4){ System.out.println("返回值为:" + method.invoke(t, "7",5)); }else{ if(modifiersNum == 130){ System.out.println("返回值为:" + method.invoke(t, new Object[] {new String[] {"M","W","Q"}})); } } } } } catch (Exception e) { // TODO: handle exception System.out.println("有异常...."); flag = true; method.setAccessible(true); } } } } } invoke会执行指定的方法,注意到多个可变参数那里要用到Object[]的二维数组
或者
import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class fun{ public static void main(String[] args){ Method[] dm = test.class.getDeclaredMethods(); //System.out.println(dm.length); test t = new test(); for(int i=0;i<dm.length;i++){ Method method = dm[i]; System.out.println("方法名称为:" + method.getName()); Class[] paramentsType = method.getParameterTypes(); System.out.println("参数类型分别为:"); for(int j =0;j<paramentsType.length;j++){ System.out.println(paramentsType[j]); } Class returnType = method.getReturnType(); System.out.println("返回值类型为:" + returnType); //System.out.println(method.getModifiers());//static=8 public=1 protected=4 private=130 String modifiersNum = Modifier.toString(method.getModifiers()); System.out.println(modifiersNum); boolean flag = true; while(flag){ try { flag = false; if(modifiersNum.equals("static")){ method.invoke(t); }else{ if(modifiersNum.equals("public")){ System.out.println("返回值为:" + method.invoke(t, 168)); }else{ if(modifiersNum.equals("protected")){ System.out.println("返回值为:" + method.invoke(t, "7",5)); }else{ if(modifiersNum.equals("private transient")){ System.out.println("返回值为:" + method.invoke(t, new Object[] {new String[] {"M","W","Q"}})); } } } } } catch (Exception e) { // TODO: handle exception System.out.println("有异常...."); flag = true; method.setAccessible(true); } } } } } 方法名称为:static_A 参数类型分别为: 返回值类型为:void static_A 方法名称为:public_B 参数类型分别为: int 返回值类型为:int public_B 返回值为:16800 方法名称为:protected_C 参数类型分别为: class java.lang.String int 返回值类型为:int protected_C 返回值为:12 方法名称为:private_D 参数类型分别为: class [Ljava.lang.String; 返回值类型为:class java.lang.String 有异常.... private_D 返回值为:MWQ
访问类的成员
类的标准名称:java.util.ArrayList 类的修饰符 : public 类的泛型参数: E 类的所有接口 java.util.List<E> interface java.util.RandomAccess interface java.lang.Cloneable interface java.io.Serializable 类的直接继承类: java.util.AbstractList<E> 类的注解: 空 import java.awt.Window.*; import java.io.ObjectInputStream.GetField; import java.lang.annotation.Annotation; import java.lang.reflect.Type; import java.lang.reflect.Modifier; import java.lang.reflect.TypeVariable; public class fun{ public static void main(String[] args) throws ClassNotFoundException{ Class<?> clazz = Class.forName("java.util.ArrayList"); System.out.println("类的标准名称:" + clazz.getCanonicalName()); System.out.println("类的修饰符 : " + Modifier.toString(clazz.getModifiers())); //输出类的泛型参数 TypeVariable<?>[] typeVariables = clazz.getTypeParameters(); System.out.println("类的泛型参数:"); if(typeVariables.length != 0){ for(TypeVariable<?> typeVariable : typeVariables){ System.out.println(typeVariable + "\t"); } }else{ System.out.println("空"); } //输出类的所有接口 Type[] interFaces = clazz.getGenericInterfaces(); System.out.println("类的所有接口"); if(interFaces.length != 0){ for(Type type : interFaces){ System.out.println("\t" + type); } }else{ System.out.println("空"); } //输出类的直接继承类 Type superClass = clazz.getGenericSuperclass(); System.out.println("类的直接继承类:"); if(superClass != null){ System.out.println(superClass); }else{ System.out.println("空"); } //输出类的所有注释信息 Annotation[] annotations = clazz.getAnnotations(); System.out.println("类的注解:"); if(annotations.length != 0){ for(Annotation annotation : annotations){ System.out.println("\t" + annotation); } }else{ System.out.println("空"); } } }
相关文章推荐
- Java反射学习 getClass()函数应用
- java反射实现javabean转json实例代码
- Java反射中常用的操作方法
- Java 反射
- java 通过反射获取调用类方法及属性
- Java高级编程 - 使用反射强制给private字段赋值
- Java高级编程 - 使用反射强制给private字段赋值
- java课堂练习,反射
- JAVA基础之反射
- java反射
- java基础反射
- Android获取apk签名信息(举例说明java反射调用方法步骤)
- 理解JAVA反射机制
- 黑马程序员---java基础加强 反射的深入理解
- 黑马程序员-----java基础java的反射机制
- java 通过反射获取调用类方法及属性
- java 反射调用带回调接口的函数
- JAVA反射
- 黑马程序员——【Java反射学习】Class类的使用
- 黑马程序员——【Java反射学习】方法的反射/成员变量的反射/构造函数的反射