高级特性七|反射机制

反射机制

反射:被视为动态语言的关键
反射机制:指在运行状态中,动态获取信息以及动态调用对象的方法的功能。
好处:
	程序运行中、操作对象
    
	解耦合,提高程序的可扩展性
应用场景:
	动态创建、操作对象
	框架内的拦截配置
动态性质:
    运行时创建对象实例
    运行时调用方法
    运行时更改属性

常用API

类名作用
Class反射的核心类,获取类的属性、方法等内容信息
Field定义类的属性,获取和设置属性的值
Method定义类的方法、获取类中方法的信息、以及执行方式
Constructor定义类的构造函数
Field提供有关类或接口的单个字段,以及对它的动态访问权限
//运行时创建对象		
		//方式一: 使用newInstance() 创建   只能调用无参构造
        Class student = Student.class;
        Student s = (Student) student.newInstance();
        System.out.println(s.toString());
        //方式二: 先获取Constructor对象,然后使用newInstance() 创建
        Class student1 = Student.class;
        //获取Student中带有String和int参数的构造方法对象
        Constructor c = student1.getConstructor(String.class,int.class);
        Student s1 = (Student) c.newInstance("张三",18);
        System.out.println(s1.toString());
方法名说 明
.getMethod(String name , Class[] params)返回此Class对象所表示的类的指定的public方法
.Method[] getMethods()返回此Class对象所表示的类的所有public方法
getDeclaredMethod(String name , Class[] prarms)返回此class对象所表示的类的指定的方法,与方法的访问级别无关
getDeclaredMethod()返回此class对象所表示的类的全部方法,与方法的访问级别无关
//获取私密性方法	
		Student s = new Student();
        Class stuclas = Student.class;
        Method m = stuclas.getMethod("study", String.class);
        m.invoke(s,"Java编程");
方法名说 明
.getXXX(obj)获取当前类型的值,该方法中Xxx对应8个基本数据类型.
.get(obj)获取指定对象上此属性的值
.setXXX(obj , value)将obj对象的该属性设置成value值(此处的Xxx对应8个基本数据类型)
.set(obj , value)将obj对象的该属性设置成value值(针对引用类型赋值)
.setAccessible(true)对获取到的属性设置访问权限
//获取初始化属性,并开放权限
		Student s = new Student();
        //获取Student类的Class对象
        Class student = Student.class;
        //获取各个访问级别的属性
        Field nameField = student.getDeclaredField("name");
        //取消权限的检查
        nameField.setAccessible(true);
        //调用set方法设置新值
        nameField.set(s,"张三");
        //获取各个访问级别的属性
        Field ageField = student.getDeclaredField("age");
        //取消权限的检查
        ageField.setAccessible(true);
        //调用set方法设置新值
        ageField.set(s,20);
        System.out.println(s.toString());
方法名说 明
.newInstance(对象或类型.class ,长度 )动态创建指定类型和长度的数组
.get(arr , 下标)返回指定数组下标值
.set(arr , 下标 , new 对象名(传参))将制定数组下标设置新值
//动态创建数组
		//动态创建Student类型的数组
        Object arr = Array.newInstance(Student.class,10);
        //存值
        Array.set(arr,3,new Student("张三",18));
        Array.set(arr,4,new Student("李四",20));
        //取值
        Student s1 = (Student) Array.get(arr,3);
        Student s2 = (Student) Array.get(arr,4);
        System.out.println(s1.toString());
        System.out.println(s2.toString());
方法名说 明
getField(String name)返回此Class对象所表示的类的指定的public属性
getField()返回此Class对象所表示的类的所有public属性
getDeclaredField(String name)返回此class对象所表示的类的指定属性,与属性的访问级别无关
getDeclaredFields()返回此Class对象所表示的类的全部属性,与属性的访问级别无关
//学员操作  综合练习
 		Pet p1 = new Pet();
        Class pet = Pet.class;
        //获取权限值
        Field nameField = pet.getDeclaredField("name");
        Field ageField = pet.getDeclaredField("age");
        Field sexField = pet.getDeclaredField("sex");
        Field loveField = pet.getDeclaredField("love");
        nameField.setAccessible(true);
        ageField.setAccessible(true);
        sexField.setAccessible(true);
        loveField.setAccessible(true);
        nameField.set(p1,"葛霄");
        ageField.set(p1,6);
        sexField.set(p1,"妙妙");
        loveField.set(p1,52.0);
        Method m = pet.getMethod("eat",String.class);
        System.out.println(p1.toString());
        m.invoke(p1,"葛霄");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值