Class对象
获取Class对象三种方式:
- 通过Object类中的getObject()方法
Person p = new Person();
Class c = p.getClass();
- 类名.class 获取到字节码文件对象
Class c2 = Person.class;
- 通过Class类中的forName()方法
Class c3 = Class.forName("com.qy.entity.Person");
实例化对象
方法一:Class类对象直接实例化
Student s1 = (Student) clazz.newInstance();
方法二:先获取构造函数,在根据构造函数实例化对象
Constructor constructor = clazz.getConstructor(String.class, int.class);
Student s2 = (Student) constructor.newInstance("雷军", 23);
成员变量 Field
返回一个成员变量
public Field getField(String name) 获取指定的 public修饰的变量
public Field getDeclaredField(String name) 获取指定的任意变量
返回多个成员变量
public Field[] getFields() 获取所有public 修饰的变量
public Field[] getDeclaredFields() 获取所有的 变量 (包含私有)
获取的Field.getName()才是字段值,直接field.toString()的字段名会带上修饰符包名类名如下图
成员方法 Method
返回获取一个方法:
public Method getMethod(String name, Class<?>... parameterTypes)
获取public 修饰的方法
public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
获取任意的方法,包含私有的。 参数1: name 要查找的方法名称; 参数2: parameterTypes 该方法的参数类型
返回获取多个方法:
public Method[] getMethods() 获取本类与父类中所有public 修饰的方法
public Method[] getDeclaredMethods() 获取本类中所有的方法(包含私有的)
构造方法 Constructor
返回一个构造方法
public Constructor<T> getConstructor(Class<?>... parameterTypes) 获取public修饰, 指定参数类型所对应的构造方法
public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) 获取指定参数类型所对应的构造方法(包含私有的)
返回多个构造方法
public Constructor<?>[] getConstructors() 获取所有的public 修饰的构造方法
public Constructor<?>[] getDeclaredConstructors() 获取所有的构造方法(包含私有的)
通过反射,调用方法
步骤:
1,获取Class对象
Class clazz = Class.forName("com.qy.entity.Student");
2,获取构造方法,创建对象
Student s1 = (Student) clazz.newInstance();
3,获取指定的public方法
Method methodSetName = clazz.getMethod("setName", String.class);
4,执行方法
methodSetName.invoke(s1,"刘强东");
反射实例化对象并且初始化值
import com.qy.entity.Student; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * 根据反射生成对象 * 方案一:类直接实例化clazz.newInstance(); * 对象的值可以根据Field设置也可以根据Method设置。 * 方案二:根据类获取构造函数,然后用构造函数实例化clazz.getConstructor(String.class, int.class); * 根据构造函数实例化对象(Student) constructor.newInstance("雷军", 23); * Created by Autumn on 2018/2/2. */ public class CreateInstanceObject { public static void main(String[] args) throws Exception { //加载class Class clazz = Class.forName("com.qy.entity.Student"); //根据class实例化一个对象 Student s1 = (Student) clazz.newInstance(); //必须要有无参构造函数,否则抛InstantiationException异常 //根据class获取成员变量,初始化对象的值 Field fieldName = clazz.getDeclaredField("name"); //getField只能获得public的字段,要想获得全部字段要用getDeclaredField Field fieldAge = clazz.getDeclaredField("age"); //将字段可读性设为true,取消java访问检查 fieldName.setAccessible(true); fieldAge.setAccessible(true); //根据字段给对象设值 fieldName.set(s1,"李彦宏"); //设置字段值set(对象,字段值) fieldAge.set(s1,21); //反射都是把对象作为参数 System.out.println(s1.toString()); //根据class获取成员方法,初始化对象的值 Method methodSetName = clazz.getMethod("setName", String.class); Method methodSetAge = clazz.getMethod("setAge", int.class); methodSetName.invoke(s1,"刘强东"); methodSetAge.invoke(s1,22); System.out.println(s1.toString()); //根据class获取构造函数,然后再根据构造函数实例化一个对象并实例化对象的值. Constructor constructor = clazz.getConstructor(String.class, int.class); //根据构造函数生成对象 Student s2 = (Student) constructor.newInstance("雷军", 23); System.out.println(s2.toString()); } }
Integer泛型的List加入String
/** * 在Integer类型List中添加String * Created by Autumn on 2018/2/2. */ public class IntListContentString { public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { //new一个int型的list List<Integer> intList = new ArrayList<Integer>(); //加入int成员 intList.add(8); intList.add(10); System.out.println(intList); //在jvm中加载class Class clazz = Class.forName("java.util.ArrayList"); //从class中获取add(String)方法 Method addMethod = clazz.getMethod("add", Object.class); //method类invoke方法(调用它的对象,方法参数) addMethod.invoke(intList, "this is a string"); System.out.println(intList); } } public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age='" + age + '\'' + '}'; } }