反射技术需要先获得类的字节码文件对象,通过字节码文件对象可以获得类的所有信息,也可以修改
- 对象名.getClass()
- 类名.class
- Class.forName(String className) className是全限定名
- ClassLoader里的loadClass(String className),不同类获取的类加载器是同一个
注意:无论通过什么方式获取的字节码文件对象 都是同一个
public class Demo2 {
public static void main(String[] args) throws ClassNotFoundException {
Student stu = new Student();
stu.getClass()
Student.class
Class.forName("com.cskaoyan._14reflect._01.Student")
ClassLoader
.getSystemClassLoader()
.loadClass("com.cskaoyan._14reflect._01.Student")
// 打印的都是一样的,同一份class对象
// class com.cskaoyan._14reflect._01.Student
// class com.cskaoyan._14reflect._01.Student
// class com.cskaoyan._14reflect._01.Student
// class com.cskaoyan._14reflect._01.Student
}
}
class Student {
}