反射获取Class对象的三种方法

2022年9月17日13:16:39

反射获取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
  • 作者:xuyin1204
  • 原文链接:https://blog.csdn.net/xuyin1204/article/details/118942590
    更新时间:2022年9月17日13:16:39 ,共 590 字。