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