Java的内省机制
2018-03-12 11:16
363 查看
内省(
首先通过
*
*
*
*
*
*
运行结果:
参考链接
Java 内省机制
java的内省机制
Java内省机制小总结
《JavaBean》-Java的内省机制
Java bean 是个什么概念?
Java内省机制
Introspector)是
Java语言对
JavaBean类属性、事件的一种缺省处理方法。
JavaBean是一种特殊的类,遵守
JavaBean的规范,即提供默认构造方法,同时所有的属性都是私有的,且每个属性都有公开的读取和设定的方法(
getter和
setter),这些方法都遵守命名的规范。例如类
Person中有属性
name,可以通过
getName/setName来得到其值或者设置新的值。
Person类:
package com.sigalhu; public class Person { private String name; private String sex; private int age; public Person() { this.name = "SigalHu"; this.sex = "男"; this.age = 26; } public Person(String name, String sex, int age) { this.name = name; this.sex = sex; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setSex(String sex) { this.sex = sex; } public String getSex() { return sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "name=" + name + ", sex=" + sex + ", age=" + age + '}'; } }
首先通过
Introspector类来获取某个对象的
BeanInfo信息,然后通过
BeanInfo来获取属性的描述器
PropertyDescriptor,通过
PropertyDescriptor就可以获取某个属性对应的
getter/setter方法,因此就可以通过反射机制来调用这些方法。此外,还可以通过构造方法直接创建
PropertyDescriptor对象来对某个对象的属性进行相应操作。
PropertyDescriptor类的主要方法有:
*
getPropertyType():获得属性的
Class对象;
*
getReadMethod():获得用于读取属性值的方法;
*
getWriteMethod():获得用于写入属性值的方法;
*
hashCode():获取对象的哈希值;
*
setReadMethod(Method readMethod):设置用于读取属性值的方法;
*
setWriteMethod(Method writeMethod):设置用于写入属性值的方法。
Main类:
package com.sigalhu; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; public class Main { static public void main(String[] args) throws Exception{ Person person = new Person("a","男",25); //如果不想把父类的属性也列出来的话,getBeanInfo的第二个参数填写父类的信息 BeanInfo beanInfo = Introspector.getBeanInfo(Person.class, Object.class); //读取所有属性值 PropertyDescriptor[] props = beanInfo.getPropertyDescriptors(); for(PropertyDescriptor prop:props){ System.out.println(prop.getName() + ": " + prop.getReadMethod().invoke(person)); } System.out.println(); //写入指定属性值 PropertyDescriptor propName = new PropertyDescriptor("name",Person.class); propName.getWriteMethod().invoke(person,"b"); System.out.println(person); System.out.println(); //设置用于写入属性值的方法 PropertyDescriptor propSex = new PropertyDescriptor("sex",Person.class); propName.setWriteMethod(propSex.getWriteMethod()); propName.getWriteMethod().invoke(person,"c"); System.out.println(person); } }
运行结果:
age: 25 name: a sex: 男 Person{name=b, sex=男, age=25} Person{name=b, sex=c, age=25}
参考链接
Java 内省机制
java的内省机制
Java内省机制小总结
《JavaBean》-Java的内省机制
Java bean 是个什么概念?
Java内省机制
相关文章推荐
- 《JavaBean》-----Java的内省机制
- java内省机制介绍
- JAVA基础 内省机制
- Java反射与内省机制总结
- JAVA的内省机制(introspector)与反射机制(reflection)
- 黑马程序员-------高薪笔记----内省机制(IntorSpector)----javabean
- 黑马程序员---java的内省机制
- 关于JAVA的内省JavaBean、类加载器、类加载器的委托机制以及代理
- java内省机制运用
- java内省机制与反射实现工厂模式
- Java基础-----JavaBean内省机制
- java的反射和内省机制
- java 内省机制
- Java内省机制
- java内省机制
- Java内省机制
- JAVA 与 MyCat(5) 类的加载 Java内省/反射机制 注解Annotation 详解
- javaWeb-javaBean+内省机制+模拟Hibernate的CRUD操作
- Java的加载资源文件+JavaBean+内省机制
- java 中的内省机制