内省(Introspector)操作JavaBean的属性
来源:互联网 发布:有内涵的小说 知乎 编辑:程序博客网 时间:2024/05/22 14:16
- 创建包package cn.itccast.introspector,在包下建javabean类Student,代码如下:
package cn.itccast.introspector;public class Student { private String name; private String password; private String email; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
- 在包cn.itccast.introspector下建Demo类,操作Student类的属性
package cn.itccast.introspector;import java.beans.BeanInfo;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.Method;import org.junit.Test;public class Demo { //通过内省Introspector的 api操作bean的属性 @Test public void test() throws Exception{ //1. 通过属性描述器PropertyDescriptor的构造方法获取属性描述器, //此处获取Student类的name属性描述器 PropertyDescriptor pd=new PropertyDescriptor("name", Student.class); //2. 获取用于写name属性值的方法,相当于setName(String name)方法 Method write=pd.getWriteMethod(); //3.创建Student对象,设置name属性为maomao Student stu=new Student(); write.invoke(stu, "maomao"); //4. 获取用于读name属性的方法,相当于getName()方法 Method reader=pd.getReadMethod(); //5. 读取stu的name属性值,并打印,查看是否设置成功 String name=(String) reader.invoke(stu,null); System.out.println(name); } @Test public void test2() throws IntrospectionException{ //操作Bean的所有属性 //1. 通过Introspector类获得Bean对象的 BeanInfo BeanInfo bif=Introspector.getBeanInfo(Student.class); //2.通过 BeanInfo 来获取属性的描述器 PropertyDescriptor PropertyDescriptor pds[]=bif.getPropertyDescriptors(); //3.通过属性描述器就可以获取某个属性对应的 getter/setter 方法 for(PropertyDescriptor pd: pds){ System.out.println(pd.getName()); } }}
0 0
- 内省(Introspector)操作JavaBean的属性
- 内省(Introspector)操作JavaBean的属性
- 内省(Introspector)操作JavaBean的属性
- 【内省】introspector操作JavaBean
- java基础之内省(Introspector)操作javabean属性的理解(主要用于开发框架)
- 通过PropertyDescriptor和Introspector对JavaBean的简单内省操作
- 内省 操作javabean的属性
- 内省操作javabean的属性
- 内省操作javabean的属性
- 【Java进阶】内省IntroSpector操作JavaBean和Apache-commons-dbutils对内省的使用
- 内省操作javabean属性
- 内省IntroSpector与JavaBean
- JavaBean和内省 IntroSpector
- 内省(introspector)和javabean
- 内省(Introspector) — JavaBean
- 使用内省(Introspector)操作bean属性
- Java--introspector(内省)操作bean属性
- 使用Java的Introspector类操作JavaBean的属性
- SQL数据库中的主键与外键介绍
- MySQL之常用命令
- es的版本和插件的版本
- Hadoop2.8.0新特性
- Viewpager + fragment,其中一些fragment不被保存在内存,切换导致初始化问题
- 内省(Introspector)操作JavaBean的属性
- 为手写UIButton、UITextField设置边框样式
- SQL SERVER 存储过程 显示26个小写字母及其对应的ASCII码值
- "Factorial Trailing Zeroes" and "Pascal's Triangle II"
- 全文检索汇总
- hdu 4424 Conquer a New Region
- PE文件的编写(一)
- 【转】ARC学习笔记:strong、weak等详解
- 我的工具集1——集中删除系统中的消息队列