获取反射类:
Student.class
student.getClass()
Class<?> aClass=Class.forName("student")
构造器
Object o = aClass.newInstance(); //默认构造器
Constructor<?> constructor = aClass.getConstructor(int.class, String.class); //指定构造器
T12 sss = (T12)constructor.newInstance(1, "sss");
Field
T12 t12 = new T12();
Field age = aClass.getField("age");
Object o = age.get(t12);
//private
T12 t12 = new T12();
Field age = aClass.getDeclaredField("age");
age.setAccessible(true);
Object o = age.get(t12);
System.out.println(o);
Method
Class<?> aClass = Class.forName("com.sjj.T12");
T12 t12 = new T12();
Method getName = aClass.getMethod("getName");
Object result = getName.invoke(t12);
System.out.println(result);
//private
Class<?> aClass = Class.forName("com.sjj.T12");
T12 t12 = new T12();
Method getName = aClass.getDeclaredMethod("getName");
getName.setAccessible(true);
Object result = getName.invoke(t12);
System.out.println(result);
标签:反射,T12,aClass,getName,Class,API,t12,age
From: https://www.cnblogs.com/sjj123/p/16842803.html