反射(reflection)
- 允许在程序执行期间,借助Reflection APi取得任何类的内部信息,并可以直接操作任意对象的内部属性及方法
- java.lang.Class:代表一个类,java.lang.reflection.*;等等是反射的主要API
关于java.lang.Class的理解
- 类加载的过程
- 程序经过javac.exe命令后,会生成一个或多个字节码文件(.class文件)
- 使用java.exe命令对某个字节码文件进行解释运行(将文案加载到内存中,即内存加载)
- 加载到内存的类.就是运行时类,就是Class的一个实例
public class Test2 {
public static void main(String[] args) throws ClassNotFoundException {
Class class1 = Person.class;
System.out.println(class1);
Person person1 = new Person();
Class class2 = person1.getClass();
System.out.println(class2);
Class class3 = Class.forName("com.gcl.exe1.Person");
System.out.println(class3);
ClassLoader classLoader = Test2.class.getClassLoader();
Class class4 = classLoader.loadClass("com.gcl.exe1.Person");
System.out.println(class4);
}
}
使用反射Load加载properties文件
public class Test3 {
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
FileInputStream fileInputStream = new FileInputStream("/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day11/src/com/gcl/exe1/jdbc.properties");
properties.load(fileInputStream);
String name = properties.getProperty("name");
String age = properties.getProperty("age");
System.out.println(name);
System.out.println(age);
ClassLoader classLoader = Test3.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("com/gcl/exe1/jdbc.properties");
properties.load(inputStream);
String name2 = properties.getProperty("name");
String age2 = properties.getProperty("age");
System.out.println(name2);
System.out.println(age2);
}
}
通过反射创建对应的运行时类的对象
public class Test1 {
public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Class clazz = String.class;
Object obj = (String) clazz.newInstance();
System.out.println(obj.getClass());
Constructor constructor = clazz.getConstructor(String.class);
Object object = (String)constructor.newInstance("你好");
System.out.println(object);
}
}
获取运行时类的属性内部结构
public class Test1 {
public static void main(String[] args) {
Class clazz = Person.class;
Field[] fields = clazz.getFields();
for (Field field: fields) {
System.out.println(field);
}
System.out.println("-------------------");
Field[] fields1 = clazz.getDeclaredFields();
for (Field field: fields1) {
System.out.println(field);
}
Field[] fields2 = clazz.getDeclaredFields();
for (Field field: fields2) {
System.out.println(field.getModifiers());
System.out.println(Modifier.toString(field.getModifiers()));
System.out.println(field.getType());
System.out.println(field.getName());
}
}
}