Java内省(基础一)
来源:互联网 发布:关口知宏 新疆妹子 编辑:程序博客网 时间:2024/06/11 02:37
程序代码:内省Person类。
package neixing;
public class Person {
private String name;
private String password;
private int age;
private String province;// 这个不是Person的属性,因为他并没有提供相应 的get或者set方法。
private String password;
private int age;
private String province;// 这个不是Person的属性,因为他并没有提供相应 的get或者set方法。
public String getName() {
return name;
}
return name;
}
public void setName(String name) {
this.name = name;
}
this.name = name;
}
public String getPassword() {
return password;
}
return password;
}
public void setPassword(String password) {
this.password = password;
}
this.password = password;
}
public int getAge() {
return age;
}
return age;
}
public void setAge(int age) {
this.age = age;
}
this.age = age;
}
public String getAddress() {
System.out.println();
return "";
}
System.out.println();
return "";
}
// 只要Person类提供了相应的get或set方法(get
// 方法有返回值,set方法接收传入的参数),那么对应的这个就是他的属性,比如说上面的那个getAddress方法也是他的属性
// 因为Person类继承自object,object有一个getClass方法,所以Person类共有5个属性
// 方法有返回值,set方法接收传入的参数),那么对应的这个就是他的属性,比如说上面的那个getAddress方法也是他的属性
// 因为Person类继承自object,object有一个getClass方法,所以Person类共有5个属性
}
----------------------------------
测试代码:
package neixing;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import org.junit.Test;
public class Demo1 {
// 内省APi操作Bean属性
@Test
public void test1() throws IntrospectionException {
BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();// 得到属性描述器
for (PropertyDescriptor pd : pds) {
System.out.println(pd);// 获知Bean到底有多少属性
// 程序输出一下内容,共四个属性
// java.beans.PropertyDescriptor[name=age; propertyType=int;
// readMethod=public int neixing.Person.getAge(); writeMethod=public
// void neixing.Person.setAge(int)]
// java.beans.PropertyDescriptor[name=address; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getAddress()]
// java.beans.PropertyDescriptor[name=age; propertyType=int;
// readMethod=public int neixing.Person.getAge(); writeMethod=public
// void neixing.Person.setAge(int)]
// java.beans.PropertyDescriptor[name=class; propertyType=class
// java.lang.Class; readMethod=public final native java.lang.Class
// java.lang.Object.getClass()]
// java.beans.PropertyDescriptor[name=name; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getName(); writeMethod=public void
// neixing.Person.setName(java.lang.String)]
// java.beans.PropertyDescriptor[name=password; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getPassword(); writeMethod=public void
// neixing.Person.setPassword(java.lang.String)]
// 这个说明了属性是由get或者set方法决定的
// 内省APi操作Bean属性
@Test
public void test1() throws IntrospectionException {
BeanInfo beanInfo = Introspector.getBeanInfo(Person.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();// 得到属性描述器
for (PropertyDescriptor pd : pds) {
System.out.println(pd);// 获知Bean到底有多少属性
// 程序输出一下内容,共四个属性
// java.beans.PropertyDescriptor[name=age; propertyType=int;
// readMethod=public int neixing.Person.getAge(); writeMethod=public
// void neixing.Person.setAge(int)]
// java.beans.PropertyDescriptor[name=address; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getAddress()]
// java.beans.PropertyDescriptor[name=age; propertyType=int;
// readMethod=public int neixing.Person.getAge(); writeMethod=public
// void neixing.Person.setAge(int)]
// java.beans.PropertyDescriptor[name=class; propertyType=class
// java.lang.Class; readMethod=public final native java.lang.Class
// java.lang.Object.getClass()]
// java.beans.PropertyDescriptor[name=name; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getName(); writeMethod=public void
// neixing.Person.setName(java.lang.String)]
// java.beans.PropertyDescriptor[name=password; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getPassword(); writeMethod=public void
// neixing.Person.setPassword(java.lang.String)]
// 这个说明了属性是由get或者set方法决定的
}
}
@Test
public void test2() throws Exception {
// ------上面中class属性是父类的属性,只是子类继承了object的getClass()方法。如果只想获取Bean自己的属性,那该怎么做??
// Open Declaration BeanInfo
// java.beans.Introspector.getBeanInfo(Class<?> beanClass, Class<?>
// stopClass) throws IntrospectionException
// 其中Class<?> stopClass表示不再内省这个类的继承树种包括这个类以及这个类的父类的所有属性。
BeanInfo beanInfo = Introspector
.getBeanInfo(Person.class, Object.class);
PropertyDescriptor[] pds2 = beanInfo.getPropertyDescriptors();// 得到属性描述器
for (PropertyDescriptor pd : pds2) {
System.out.println(pd);// 获知Bean到底有多少属性
public void test2() throws Exception {
// ------上面中class属性是父类的属性,只是子类继承了object的getClass()方法。如果只想获取Bean自己的属性,那该怎么做??
// Open Declaration BeanInfo
// java.beans.Introspector.getBeanInfo(Class<?> beanClass, Class<?>
// stopClass) throws IntrospectionException
// 其中Class<?> stopClass表示不再内省这个类的继承树种包括这个类以及这个类的父类的所有属性。
BeanInfo beanInfo = Introspector
.getBeanInfo(Person.class, Object.class);
PropertyDescriptor[] pds2 = beanInfo.getPropertyDescriptors();// 得到属性描述器
for (PropertyDescriptor pd : pds2) {
System.out.println(pd);// 获知Bean到底有多少属性
// 打印结果表明不再内省父类Object 的Class属性
// java.beans.PropertyDescriptor[name=address; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getAddress()]
// java.beans.PropertyDescriptor[name=age; propertyType=int;
// readMethod=public int neixing.Person.getAge(); writeMethod=public
// void neixing.Person.setAge(int)]
// java.beans.PropertyDescriptor[name=class; propertyType=class
// java.lang.Class; readMethod=public final native java.lang.Class
// java.lang.Object.getClass()]
// java.beans.PropertyDescriptor[name=name; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getName(); writeMethod=public void
// neixing.Person.setName(java.lang.String)]
// java.beans.PropertyDescriptor[name=password; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getPassword(); writeMethod=public void
// neixing.Person.setPassword(java.lang.String)]
// java.beans.PropertyDescriptor[name=address; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getAddress()]
// java.beans.PropertyDescriptor[name=age; propertyType=int;
// readMethod=public int neixing.Person.getAge(); writeMethod=public
// void neixing.Person.setAge(int)]
// java.beans.PropertyDescriptor[name=class; propertyType=class
// java.lang.Class; readMethod=public final native java.lang.Class
// java.lang.Object.getClass()]
// java.beans.PropertyDescriptor[name=name; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getName(); writeMethod=public void
// neixing.Person.setName(java.lang.String)]
// java.beans.PropertyDescriptor[name=password; propertyType=class
// java.lang.String; readMethod=public java.lang.String
// neixing.Person.getPassword(); writeMethod=public void
// neixing.Person.setPassword(java.lang.String)]
}
}
@Test
public void test3() throws Exception {
public void test3() throws Exception {
// 获取属性最终是为了操作属性,如何操作?
Person p = new Person();
// 操作age属性
PropertyDescriptor pdDescriptor = new PropertyDescriptor("age",
Person.class);// 第一个是属性名
// 这样pdDescriptor就专门用来操作age属性.操作无非就是写数据和读数据
Method method = pdDescriptor.getWriteMethod();// ---->public void
// setAge(int age);
method.invoke(p, 45);
System.out.println(p.getAge());// 成功打印
// 获取属性的值
Method methodRead = pdDescriptor.getReadMethod();// ---->public void
// getAge()
int ageRead = (Integer) methodRead.invoke(p, null);// get方法不需要接收参数
System.out.println(ageRead);
}
Person p = new Person();
// 操作age属性
PropertyDescriptor pdDescriptor = new PropertyDescriptor("age",
Person.class);// 第一个是属性名
// 这样pdDescriptor就专门用来操作age属性.操作无非就是写数据和读数据
Method method = pdDescriptor.getWriteMethod();// ---->public void
// setAge(int age);
method.invoke(p, 45);
System.out.println(p.getAge());// 成功打印
// 获取属性的值
Method methodRead = pdDescriptor.getReadMethod();// ---->public void
// getAge()
int ageRead = (Integer) methodRead.invoke(p, null);// get方法不需要接收参数
System.out.println(ageRead);
}
/**
* 获取当前属性的类型
*
* @throws IntrospectionException
*
*/
@Test
public void test4() throws IntrospectionException {
// 操作age属性
PropertyDescriptor pdDescriptor = new PropertyDescriptor("age",
Person.class);// 第一个是属性名
// 查看age属性的类型
Class classz = pdDescriptor.getPropertyType();
System.out.println(classz);// 打印:int
}
* 获取当前属性的类型
*
* @throws IntrospectionException
*
*/
@Test
public void test4() throws IntrospectionException {
// 操作age属性
PropertyDescriptor pdDescriptor = new PropertyDescriptor("age",
Person.class);// 第一个是属性名
// 查看age属性的类型
Class classz = pdDescriptor.getPropertyType();
System.out.println(classz);// 打印:int
}
}
0 0
- Java内省(基础一)
- java内省(基础二)
- Java基础加强之内省(introspector)
- Java基础加强之内省(introspector)
- Java基础加强---内省
- java基础之内省
- JAVA基础加强:内省
- Java基础--反射、内省
- JAVA基础 内省机制
- java基础之内省
- 反射与内省(一)Java
- java基础加强_JavaBean内省
- Java基础视频教程-JavaBean内省
- Java基础---内省、注解、类加载器 (黑马程序员)
- Java 高新技术(内省)
- Java Introspector(内省)
- 基础加强-IntroSpector(内省)
- java基础学习要点七:内省
- Java读书笔记03 输入输出
- SVM(二)线性分类器的求解——问题的描述
- 嵌入式行业需要掌握的东西
- 利用Struts1实现多个文件的上传
- 黑马程序员-C语言视频日记-流程控制(1)选择结构
- Java内省(基础一)
- 使用OpenSessionInViewFilter解决session关闭问题
- 2010年山东省第一届ACM大学生程序设计竞赛——Emergency
- Leetcode之LRU Cache
- Java读书笔记04 控制流程 大数值 数组
- Drawing with Data-D3
- java内省(基础二)
- C里面的二维数组的初始化问题
- .net C# ADSL 断线重连