zoukankan      html  css  js  c++  java
  • day27(反射之内省机制)

      内省

          内省:底层是使用反射机制实现的,是对于反射的进一步封装。

            反射:通过类名来获取类中的所有属性和方法及类中的所有隐藏的方法。

            内省:通过一个标准类(javabean类)来获取bean中的字段、get/set方法。

    JavaBean:就是一个满足了特定格式的Java类:
    * 需要提供无参数的构造方法:
    * 属性私有
    * 对私有的属性提供public的get/set方法.
    

          创建一个Student标准类

    public class Student {
    	private String name;
    	private int age;
    	//private String sax;
    	public String getSax() {
    		return null;
    	}
    	public void setSax(String sax) {
    		//this.sax = sax;
    	}
    	public Student() {
    		super();
    		// TODO Auto-generated constructor stub
    	}
    
    	public Student(String name, int age) {
    		super();
    		this.name = name;
    		this.age = 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;
    	}
    }
    

          测试类

        public static void main(String[] args) throws IntrospectionException {
    		//获取的bean信息
    		BeanInfo beanInfo = Introspector.getBeanInfo(Student.class);
    		//回去bean的属性描述
    		PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
    		for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
    			//获取属性名字(只要提供get/set属性时)
    			System.out.println(propertyDescriptor.getName());
    			//获得get方法
    			//System.out.println(propertyDescriptor.getReadMethod().getName());
    			// 获得set方法
    			//propertyDescriptor.getWriteMethod();.
    		}
    	}
    

      

          

        

  • 相关阅读:
    C#学习笔记之——矩形覆盖问题
    链表,栈,队列代码
    链表练习代码
    2012年全国计算机专业大学排名
    寄存器介绍
    win8 wifi开关显示关闭,且设置里面wifi开关显示灰色的解决办法
    360随身wifi无法使用临时解决方案大全
    锐捷客户端的校园网电脑如何转化成无线路由
    未完成数据结构题目
    数据结构代码1
  • 原文地址:https://www.cnblogs.com/fjkgrbk/p/introspection.html
Copyright © 2011-2022 走看看