java高新技术之使用反射和内省对对象的操作
来源:互联网 发布:沈阳居住证网络查询 编辑:程序博客网 时间:2024/06/07 23:37
先定义一个学生类:
类中具备getter、setter
class Student {private String name;private int age;public Student(String s, int a){this.name = s;this.age = a;}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;}}
分别使用反射和内省对对象的操作:
import java.beans.PropertyDescriptor;import java.lang.reflect.Method;public class IntroSpectorTest {public static void main(String[] args) throws Exception {Student s = new Student("zhangsan", 20);Student s1 = new Student("lisi", 20);//通过反射获得s的各项值Method methodGetName = s.getClass().getMethod("getName");String name = (String) methodGetName.invoke(s);System.out.println(name);//通过内省获得...PropertyDescriptor pd = new PropertyDescriptor("name", s.getClass());Method methodGetName1 = pd.getReadMethod();String name1 = (String) methodGetName1.invoke(s);System.out.println(name1);System.out.println("-----------");//继续使用反射对s对象赋值Method methodSetNmae = s.getClass().getMethod("setName", String.class);methodSetNmae.invoke(s, "lisi");System.out.println(s.getName());//继续使用内省对s对象赋值Method methodSetName1 = pd.getWriteMethod();methodSetName1.invoke(s1, "zhangsan");System.out.println(s1.getName());System.out.println("-------------");//对其他属性操作PropertyDescriptor pd1 = new PropertyDescriptor("age", s.getClass());Method methodGetAge = pd1.getReadMethod();System.out.println(methodGetAge.invoke(s1));Method methodSetAge = pd1.getWriteMethod();methodSetAge.invoke(s1, 23);System.out.println(s1.getAge());}}
两者分别是怎样实现对元素的操作的呢?
反射:
1、获得某类的某个具体操作方法(通过类.class文件调用方法,并传入方法名和参数获得)
2、使用获得的方法操作属性(调用得到的方法传入对象以及需要的参数操作属性)
内省:
1、获得某对象的某一属性(通过传入属性名称,类.class获得相应的属性对象)
2、获得某类的某个具体操作方法(通过属性对象获得get/set方法)
3、使用获得的方法操作属性(同反射)
提取方法---优化后的内省操作对象属性代码:
import java.beans.IntrospectionException;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class IntroSpectorTest {public static void main(String[] args) throws Exception {Student s = new Student("zhangsan", 20);//通过内省获得属性值String propertyName = "name";Object retVal = getProperty(s, propertyName);System.out.println("s对象的名字"+retVal);//使用内省对属性赋值setProperty(s, propertyName);System.out.println("设置后的名字:"+s.getName());}//内省中提取出来的设置属性的方法private static void setProperty(Student s, String propertyName)throws IntrospectionException, IllegalAccessException,InvocationTargetException {PropertyDescriptor pd = new PropertyDescriptor(propertyName, s.getClass());Method methodSetName = pd.getWriteMethod();methodSetName.invoke(s, "lisi");}//内省中提取出来的获取属性的方法private static Object getProperty(Student s, String propertyName)throws IntrospectionException, IllegalAccessException,InvocationTargetException {PropertyDescriptor pd = new PropertyDescriptor(propertyName, s.getClass());Method methodGetName1 = pd.getReadMethod();Object retVal = methodGetName1.invoke(s);return retVal;}}
通过遍历某一.class获得属性:
//内省中通过遍历某一.class获得属性private static Object getProperty(Student s, String propertyName)throws IntrospectionException, IllegalAccessException,InvocationTargetException {Object retVal = null;BeanInfo beanInfo = Introspector.getBeanInfo(s.getClass());PropertyDescriptor[] prop = beanInfo.getPropertyDescriptors();for(PropertyDescriptor p : prop){if(p.getName().equals(propertyName)){Method methodGetName = p.getReadMethod();retVal = methodGetName.invoke(s);break;}}return retVal;}
0 0
- java高新技术之使用反射和内省对对象的操作
- 使用反射和内省对对象的操作
- 黑马程序员 Java高新技术--反射和内省
- Java高新技术之内省操作(BeanUtils、PropertyUtils和ConverUtils.register)
- Redis对对象的操作
- 对对象的简单操作
- java之反射和内省
- java的内省和反射
- java的反射和内省
- Java对对象的序列化和反序列化
- Java对对象的序列化和反序列化
- Java对对象的序列化和反序列化
- 【java反射之】对javabean内省操作
- 黑马程序员 高新技术<二> 反射和内省
- java资源表示之------File对对象
- Java对对象进行保存和读取
- java List对对象的属性排序
- Java对对象的4种引用
- java操作excel-jxl
- 顺序图知识点
- FF,ie6,ie7兼容性
- PHP 脚本执行内存使用大小
- jEasyUI 中文版
- java高新技术之使用反射和内省对对象的操作
- 赋值操作符概要
- API Hook基本原理和实现
- C++中星号(*)和与号(&)的用法
- 研大考研不是骗子,五一试学感悟
- 8080 和 6800 接口的区别
- C# download file from webserver
- 获得指向关键ios目录的引用
- OCP 1Z0 051 QUESTION NO: 55