标签:取值 getc ssi set 调用函数 ble str null end
简单的说,反射技术就是动态的获取指定的类以及动态的调用类的内容。可以提高扩展性
Boolean var1 = true; Class<?> classType2 = var1.getClass();
Class<?> classType4 = Boolean.class; System.out.println(classType4);
Class<?> classType5 = Class.forName("java.lang.Boolean");
System.out.println(classType5);
Class<?> classType3 = Boolean.TYPE;
System.out.println(classType3);
public Field getField(String name)
public Field[] getFields()
public Method getMethod(String name, Class<?>... parameterTypes)
public Method[] getMethods()
public Constructor<T> getConstructor(Class<?>... parameterTypes)
public Constructor<?>[] getConstructors()
Class<?> classType = ExtendType.class; Object inst = classType.newInstance();
Class<?> classType = ExtendType.class; Constructor<?> constructor1 = classType.getConstructor(); Object inst = constructor1.newInstance(); System.out.println(inst);
Constructor<?> constructor2 = classType.getDeclaredConstructor(int.class, String.class); Object inst = constructor2.newInstance(1, "123"); System.out.println(inst);
Class<?> clazz = Class.forName("Person"); Object obj = clazz.newInstance(); String methodName = "show"; Method method = clazz.getMethod("show", String.class, int.class); method.invoke(obj, "zhang", 11);
Class<?> clazz = Class.forName("Person"); Object obj = clazz.newInstance(); String methodName = "staticShow"; Method method = clazz.getMethod(methodName, null); method.invoke(obj, null);
Class<?> clazz = Class.forName("Person"); // Object obj = clazz.newInstance(); // 没有参数的 Constructor<?> cons = clazz.getConstructor(String.class, int.class); Object obj = cons.newInstance("zhang", 11);// 带参数的 String filename = "age"; // Field field = clazz.getField(filename);// 获取公共字段,如果是private 会报异常 Field declaredField = clazz.getDeclaredField(filename);// 获取申明的字段 declaredField.setAccessible(true); // 设置java 的访问限制,如果为false,则私有变量不能访问 declaredField.set(obj, 22); System.out.println(declaredField.get(obj));
标签:取值 getc ssi set 调用函数 ble str null end
原文地址:https://www.cnblogs.com/king-peng/p/10049983.html