反射
程序在运行期间借助反射 API 取得任何类的内部信息,并通过这些内部信息去操作对应对象的内部属性和方法
获取 class 对象的方式
1、类的 class 属性
1 | Class<Person> cl1 = Person.class; |
2、Object 对象 的 getClass() 方法
1 | Class<Person> cl2 = (Class<Person>) new Person().getClass(); |
3、通过 Class 类的 forName() 方法(最常用)
1 | Class cl3 = Class.forName("com.model.Person"); |
4、通过 ClassLoader 类(不常用)
1 | Class cl4 = Person.class.getClassLoader().loadClass("com.model.Person"); |
Simple is Awesome