1.什么是反射?
反射是一种机制,利用反射机制动态的实例化对象、读写属性、调用方法、构造函数。
//
//2.传统实例化对象与反射机制实例化对象的区别?
//1)传统方式
/* Hello hello =new Hello();
hello.setName("张三");
System.out.println(hello.getName());
//2)反射方式
Class cls =Hello.class;
Object hello1 =cls.newInstance();
System.out.println(hello);*/
//前提:一切与反射先关的操作都从获取类对象开始!!!(3种)
//1)类名.class
//Class cls=Hello.class;
//2)实例化对象名.getClass()
/*Hello h =new Hello();
Class cls =h.getClass();*/
//3)Class.forName(类的全路径名,权限定名)
/*Class cls =Class.forName("com.zking.entity.Hello");
System.out.println("类的全路径名:"+cls.getName());
System.out.println("获取简单类名:"+cls.getSimpleName());
System.out.println("获取反问修饰符:"+cls.getModifiers());
System.out.println("获取类所在的包名:"+cls.getPackage());*/
//获取对象
Class cls =Class.forName("com.zking.utils.Student");
//反射机制实例化对象
Student stu =(Student) cls.newInstance();
2.如何得到类对象
一切反射相关的代码都从获得类对象开始
3种获取方式:
2.1 类名.class;
2.2 对象名.getClass();
2.3 Class.forName(全限定名/全路径名)
3.根据类得到类名(全限定名/全路径名)
1)cName.getName(); -->获取全限定名
2)cName.getSimpleName(); -->获取类名
3)cName.getPackage(); -->获取包名
4.根据类得到类的属性
Field field=cla.getField(“属性名”);
field.getName(); -->获取属性名
filed.getType(); -->获取属性类型
field.getModifiers(); -->获取属性访问修饰符
field.set(Object,Object); -->设置属性值,参数1:要设置属性所在对象;参数2:要设置的值;
field.get(Object); -->获取属性值,参数:要获取属性值的对象
field.getDeclaredField(“属性名”); -->获取单个属性(私有、公有、受保护、默认、静态)
field.getDeclaredFields(); -->获取所有属性(私有、公有、受保护、默认、静态)
//3.获取属性(Field)
//3.1)获取单个公共的属性
//Field field =cls.getField("age");
//3.2)获取单个公共的,私有的,保护的等等属性。
//Field field =cls.getDeclaredField("sex");
//3.3)获取所以公共的属性
//Field[] fields =cls.getFields();
//3.4)获取所以公共的,私有的,保护的等等。
//Field[] fields =cls.getDeclaredFields();
/* for (String field : args) {
//System.out.println("获取属性名:"+field.getName());
//System.out.println("获取属性的访问修饰符:"+field.getModifiers());
//System.out.println("获取属性类型:"+field.getType());
//System.out.println("------------------------------");
5.根据类得到类的方法
//无参无返回、无参有参会、有参无返回、有参有返回
cla.getMethod(); -->获取单个公有方法
cla.getDeclaredMethod(); -->获取当个方法(包括私有、受保护、默认、公有)
cla.getMethods(); -->获取所有公有方法
cla.getDeclaredMethods(); -->获取所有的方法(包括私有、受保护、默认、公有)
6.根据类得到类的构造方法
cla.getConstrutor(); -->获取单个公有构造方法
cla.getDeclaredConstrutor(); -->获取单个构造方法(包括私有、受保护、默认、公有)
cla.getConstrutors(); -->获取所有的公有构造方法
cla.getDeclaredConstrutors(); -->获取所有的构造方法(包括私有、受保护、默认、公有)
7.根据类得到类的实现接口列表
Class[] interface=cla.getInterfaces(); -->获取类对象中所有实现接口列表
Object obj =field.get(stu);
System.out.println(obj);*/
//4.调用方法
//有参又返回,有参无返回,无惨又返回,无惨无返回
//4.1)获取单个公共的方法
//参数1:方法名
//参数2:被调用方法的参数
//Member methods =cls.getMethod("hello", null);
//4.2)获取单个公共的,私有的,保护的等等方法
//Method method =cls.getDeclaredMethod("hello", String.class);
//4.3)获取所有的公共方法
/*Method[] methods =cls.getDeclaredAnnotations();
for (Method method : methods) {
System.out.println("获取方法名:"+method.getName());
System.out.println("获取方法的访问修饰符:"+method.getModifiers());
System.out.println("获取方法的参数数量:"+method.getParameterCount());
System.out.println("获取方法的返回类型:"+method.getReturnType());
}*/
//4.5)调用方法
//Method method =cls.getMethod("hello", String.class);
Method method=cls.getDeclaredMethod("add", Integer.class,Integer.class);
//设置访问权限
//method.setAccessible(true);
//执行方法
//参数1:实例化对象名
//参数2:方法参数
//Object returnValue =method.invoke(stu, 20,10);
//System.out.println(returnValue);
//调用构造函数
Constructor cons =cls.getDeclaredConstructor(Integer.class);
cons.setAccessible(true);
cons.newInstance(10);
}