三种获取反射的方法
public class Student{
private int id;
private String name;
private float score;
}
public class Text{
public static void main(){
Student stu = new Student();
Class stu_obj1 = stu.getClass();//第一种是对象通过getclass()方法获得class对象
System.out.println(stu_obj1.getName());
Class stu_obj2 = Class.forName("cn.sxt.domain.Student")//第二种是通过forname方法获得class对象
Class stu_obj3 = Student.class; //第三种通过类名+class后缀来获得class对象
}
}
网友评论