反射获取Class对象的三种方法
package reflect;
public class reflectTest {
public static void main(String[] args) throws Exception {
//第一种方式获取Class对象
Student stu1 = new Student();// 这一new,产生一个Student对象和一个Class对象。
Class stuClass1 = stu1.getClass();// 获取Class对象
System.out.println(stuClass1.getName());
//第二种方式获取Class对象
Class stuClass2 = Student.class;
System.out.println(stuClass2.getName());
System.out.println(stuClass1 == stuClass2);
// 第三种方式获取Class对象
Class stuClass3 = Class.forName("reflect.Student");
System.out.println(stuClass3.getName());
System.out.println(stuClass2 == stuClass3);
}
}
运行结果:
reflect.Student
reflect.Student
true
reflect.Student
true