首先:
1.加载类:
//练习获取字节码对象的3种方式 //Class<Student> studentClass = Student.class; //Class<? extends Student> aClass = new Student().getClass(); Class<?> clazz = Class.forName("TestT.Student");
2.获取类的构造器:
//6.通过字节码对象获取目标类Student的构造方法 //Constructor<?>[] cs = clazz.getConstructors(); //for(Constructor str :cs ){ // System.out.println(str.getName());//打印本轮遍历到的构造方法的名字 // Class[] ps = str.getParameterTypes();//通过本轮遍历到的构造函数对象获取构造函数的参数类型 // System.out.println(Arrays.toString(ps));//打印参数类型 //} //获得无参构造 Constructor<Student> de = stu.getDeclaredConstructor(); System.out.println(de.getName()+"->"+de.getParameterCount()); //获得全参数构造 Constructor<Student> de1 = stu.getDeclaredConstructor(String.class, int.class); System.out.println(de1.getName()+"->"+de1.getParameterCount());
3.获取类的构造器的作用:
调用无参构造
Class<Student> stu = Student.class; //获得无参构造 Constructor<Student> de = stu.getDeclaredConstructor(); System.out.println(de.getName()+"->"+de.getParameterCount()); de.setAccessible(true);//禁止检查权限访问 Student student = de.newInstance(); System.out.println(student);
调用有参数构造
Class<Student> stu = Student.class; //获得全参数构造 Constructor<Student> de1 = stu.getDeclaredConstructor(String.class, int.class); System.out.println(de1.getName()+"->"+de1.getParameterCount()); Student student1 = de1.newInstance("小明", 18); System.out.println(student1);
4.获取类的成员变量:
获取类的全部成员变量:
Class<Student> stu = Student.class; //获取类的全部成员变量 Field[] fields = stu.getDeclaredFields(); for (Field field : fields) { System.out.println(field.getName()+"->"+field.getType()); }
获取某个成员变量:
Class<Student> stu = Student.class; //获取某个成员变量 Field name = stu.getDeclaredField("name"); System.out.println(name.getName()+"->"+name.getType()); Field age = stu.getDeclaredField("age"); System.out.println(age.getName()+"->"+age.getType());
5.获取成员变量的作用:
为成员变量赋值和取值:
//为成员变量赋值 Student s=new Student(); name.setAccessible(true); name.set(s,"Java大牛"); System.out.println(s); //取值 String n = (String)name.get(s); System.out.println(n);
6.获取类的成员方法:
获取所有方法:
Class<Student> stu = Student.class; //获取类的方法: Method[] methods = stu.getDeclaredMethods(); for (Method method : methods) { System.out.println(method.getName()+"->"+ method.getParameterCount()+"->"+ method.getReturnType());
获取某个方法(无参数):
Class<Student> stu = Student.class; //获取某个方法(无参数) Method play = stu.getDeclaredMethod("play"); System.out.println(play.getName()+"->"+ play.getParameterCount()+"->"+ play.getReturnType());
获取某个方法(有参数):
Class<Student> stu = Student.class; //获取某个方法(有参数) Method sunDay = stu.getDeclaredMethod("sunDay", int.class); System.out.println(sunDay.getName()+"->"+ sunDay.getParameterCount()+"->"+ sunDay.getReturnType());
7.获取类的成员方法的作用:
无参数:
Student s=new Student(); //获取某个方法(无参数) Method play = stu.getDeclaredMethod("play"); System.out.println(play.getName()+"->"+ play.getParameterCount()+"->"+ play.getReturnType()); //成员方法的作用: Object invoke = play.invoke(s); System.out.println(invoke);
有参数:
Student s=new Student(); //获取某个方法(有参数) Method sunDay = stu.getDeclaredMethod("sunDay", int.class); System.out.println(sunDay.getName()+"->"+ sunDay.getParameterCount()+"->"+ sunDay.getReturnType()); //成员方法的作用: Object invoke1 = sunDay.invoke(s, 1); System.out.println(invoke1);