import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
public class IntrospectorDemo { public static void main(String[] args) {
// TODO Auto-generated method stub
IntrospectorDemo id=new IntrospectorDemo();
try {
id.test1();
id.test2();
id.test3();
id.test4();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public class Person
{
public Person(){
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
private String name;
private int age;
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;
} } //获得person类的所有bean属性 public void test1() throws Exception{
BeanInfo info = Introspector.getBeanInfo(Person.class);
PropertyDescriptor[] pds = info.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
System.out.println(pd.getPropertyType()+" "+pd.getName());
}
}
//获得Person(不包含Object类)的所有bean属性 public void test2() throws Exception{
BeanInfo info = Introspector.getBeanInfo(Person.class,Object.class);
PropertyDescriptor[] pds = info.getPropertyDescriptors();
for(PropertyDescriptor pd:pds){
System.out.println(pd.getPropertyType()+" "+pd.getName());
}
}
//设置Person的age属性 public void test3() throws Exception{
Person p = new Person();
PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);
Method method = pd.getWriteMethod();
method.invoke(p, 20);
System.out.println(p.getAge());
}
//调用Person的getAge() public void test4() throws Exception{
Person p = new Person("xiazdong",30);
PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);
Method method = pd.getReadMethod();
System.out.println(method.invoke(p, null));
} }

最新文章

  1. 转数据库分库分表(sharding)系列(二) 全局主键生成策略
  2. 关于LED 流水灯的软件调试方法(非开发板调试)
  3. easyui accordion—手风琴格子始终展开和多个格子展开
  4. Tensorflow 上手——手写数字识别
  5. Day4 内置函数补充、装饰器
  6. 跨平台utf8转unicode研究实现(2)
  7. 学习笔记——工厂模式Factory
  8. CSS动画效果的回调
  9. 老男孩Python全栈学习 S9 日常作业 013
  10. 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用) ubuntu 安装vim 及遇到的错误处理
  11. 记AOP概念理解
  12. HTML5开发和布局(待补充)
  13. samba服务器之无认证进入共享目录
  14. QT程序打包发布
  15. 创建react项目
  16. # 20155319 Exp3 免杀原理与实践
  17. ASP.NET MVC@model多个对象
  18. 反射中Class.forName()和ClassLoader.loadClass()的区别
  19. mahout版本兼容问题
  20. Hello World on Impala

热门文章

  1. window安装特定补丁(勒索病毒)
  2. Linux用户与用户组
  3. 【codeforces 370C】Mittens
  4. Qt之QSpacerItem
  5. 关于ValueAnimation以及Interpolator +Drawable实现的自己定义动画效果
  6. JAVA:从public static void main(String args[])開始
  7. UITextField限制输入长度
  8. App server 与 Web server之间的区别
  9. java9新特性-8-语法改进:钻石操作符(Diamond Operator)使用升级
  10. 基于JavaSwing的例子-非连接数据库