一.什么是反射?
反射是一种机制,利用反射机制动态的实例化对象、读写属性、调用方法、构造函数。
二.如何得到类对象
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(); -->获取所有属性(私有、公有、受保护、默认、静态)5.根据类得到类的方法
//无参无返回、无参有参会、有参无返回、有参有返回
cla.getMethod(); -->获取单个公有方法
cla.getDeclaredMethod(); -->获取当个方法(包括私有、受保护、默认、公有)
cla.getMethods(); -->获取所有公有方法
cla.getDeclaredMethods(); -->获取所有的方法(包括私有、受保护、默认、公有)6.根据类得到类的构造方法
cla.getConstrutor(); -->获取单个公有构造方法
cla.getDeclaredConstrutor(); -->获取单个构造方法(包括私有、受保护、默认、公有)
cla.getConstrutors(); -->获取所有的公有构造方法
cla.getDeclaredConstrutors(); -->获取所有的构造方法(包括私有、受保护、默认、公有)7.根据类得到类的实现接口列表
Class[] interface=cla.getInterfaces(); -->获取类对象中所有实现接口列表
package com.zking.reflect.util;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import com.zking.reflect.entity.Student;
public class Demo1 {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException, NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
// 1.什么是反射
// 反射是一种机制,利用反射机制可以动态实例化对象 读写属性 调用方法以及构造函数
// 异常:
// 1) InstantiationException:反射机制实例化异常
// 2) ClassNotFoundException:类没有发现异常
// 3) NoSuchFieldException:没有匹配的属性异常
// 4) IllegalAccessException:没有访问权限异常
// 5) NoSuchMethodException:没有匹配的方法异常
// 2.传统实例化
// Hello hello=new Hello();
// hello.setName("李四");
// System.out.println(hello.getName());
// 反射方式
// Class cls=Hello.class;
// Object hello1 = cls.newInstance();
// System.out.println(hello1);
// 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.reflect.entity");
//
//
// 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.reflect.entity.Student");
//反射机制实例化对象
Student su = (Student) cls.newInstance();
//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 (Field field : fields) {
// System.out.println("获取属性名:"+field.getName());
// System.out.println("获取属性的访问修饰符:"+field.getModifiers());
// System.out.println("获取属性类型:"+field.getType());
// System.out.println("--------------------------------");
// }
//3.5) 赋值
//Field field = cls.getDeclaredField("sid");
//设置访问权限
//field.setAccessible(true);
//赋值方法
//参数1:实例化对象名
//参数2:赋值的值
//field.set(su, "p007");
//3.6) 取值
//取值方法
//参数:实例化对象名
//Object obj = field.get(su);
//System.out.println(obj);
//4.调用方法 (Method)
//有参有返回,有参无返回,无参无返回,无参又返回
//4.1) 获取单个公共的方法
//参数1:方法名
//参数2:被调用方法的参数
//Method method=cls.getMethod("hello", null);
//4.2) 获取单个公共的,私有的,受保护的等方法
//Method method = cls.getDeclaredMethod("hello", String.class);
//4.3) 获取所有公共的方法
//Method[] methods = cls.getMethods();
//4.4) 获取所有公共的,私有的,受保护的,最终的等方 法
// Method[] methods = cls.getDeclaredMethods();
// for (Method method : methods) {
// System.out.println("获取方法名:"+method.getName());
// System.out.println("获取方法的访问修饰符:"+method.getModifiers());
// System.out.println("获取方法的参数数量:"+method.getParameterCount());
// System.out.println("获取方法的返回类型:"+method.getReturnType());
// System.out.println("--------------------------------");
// }
//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(su, 20,10);
//System.out.println(returnValue);
//5.调用构造函数
//Constructor cons = cls.getConstructor(String.class);
//cons.newInstance("李四");
Constructor cons = cls.getDeclaredConstructor(Integer.class);
cons.setAccessible(true);
cons.newInstance(10);
}
}