Java学习笔记十

。。。主义者的痛苦和坚强!

今日整理记录内容:
1、反射(reflector)
2、JavaBean类
3、内省(introspector)

一、反射
1、反射就是把Java类中的各种成分(方法Method、成员变量Field、构造方法Contructor、包Package等等)映射成为相应的Java类
2、反射操作的是字节码
3、反射常用于框架当中。
4、子类中复写了父类中的某个属性, 那么子类的字节码中就会有该属性.
解释:


反射操作类
public class ReflectionPoint {
	
	private int age = -1;
	private String name = "没有名字";
	public boolean oBrFor = false;
	
	public ReflectionPoint(){}
	private ReflectionPoint(int age){this.age = age;}
	public ReflectionPoint(int age, String name){
		this.age = age;
		this.name = name;
	}
	
	private int addAge(int age){
		return ++age;		
	}
	
	private void changeName(String newName){
		name = newName;
	}
	
	public String getName(){
		return name;
	}
	public int getAge(){
		return age;
	}	
}

反射的简单使用
public class ReflectionPoint {
	
	private int age = -1;
	private String name = "没有名字";
	public boolean oBrFor = false;
	
	public ReflectionPoint(){}
	private ReflectionPoint(int age){this.age = age;}
	public ReflectionPoint(int age, String name){
		this.age = age;
		this.name = name;
	}
	
	private int addAge(int age){
		return ++age;		
	}
	
	private void changeName(String newName){
		name = newName;
	}
	
	public String getName(){
		return name;
	}
	public int getAge(){
		return age;
	}	
}

输出结果:
没有名字
22
23
false
true
郝采平
-1
没有名字
false
没有名字
23

二、JavaBean类
1、JavaBean类通常是对数据进行设置和获取操作的。
2、JavaBean的实例对象通常称之为值对象(ValueObject 简称VO)
3、JavaBean类必须是具体的和公共的,并且具有无参数的构造器
4、JavaBean类通常需要被串行化,方便类对象当作数据一样被传递。
解释:

下面就是一个标准的javaBean类
public class JavaBeanTest implements Serializable{

	private int age = -1;
	private String name = "没有名字";
	private boolean sure = false;
	
	public JavaBeanTest(){}
	
	public JavaBeanTest(int age){}

	public JavaBeanTest(int age, String name, boolean sure) {
		super();
		this.age = age;
		this.name = name;
		this.sure = sure;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isSure() {
		return sure;
	}

	public void setSure(boolean sure) {
		this.sure = sure;
	}	
}

通过上面的javaBeanTest类,我们可以看到JavaBean类的创建规则:类首先被串行化(Serializable),每一个私有变量都对应着getXxx()和setXxx()方法,对于boolean类型的变量对应着的是isXxx()和setXxx()方法。

三、内省(introspector)
主要是用于对Javabean操作的技术,在你不了解bean类的创建规则的情况下,采用内省的方式对bean的属性进行读取和修改
解释:

采用内省的方式操作javaBeanTest类
public class IntrospectorTest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

         try {
        	 /*获得某个属性,并设置该属性的属性值然后输出该属性值。*/
        	JavaBeanTest jbt1 = new JavaBeanTest();
            String propertyName = "age";//属性名
			PropertyDescriptor pd1 = new PropertyDescriptor(propertyName, JavaBeanTest.class);
		    Method methodWrite1 = pd1.getWriteMethod();
		    methodWrite1.invoke(jbt1, 22);
		    Method methodRead1 = pd1.getReadMethod();
		    System.out.println(methodRead1.invoke(jbt1, null));
            
		    /*拿到JavaBeanTest所以属性,并输出 名字为 name 的属性*/
		    JavaBeanTest jbt2 = new JavaBeanTest();
		    BeanInfo bf1 = Introspector.getBeanInfo(JavaBeanTest.class);
		    PropertyDescriptor[] pd2 = bf1.getPropertyDescriptors();
		    for (PropertyDescriptor p : pd2) {
				if(p.getName().equals("name")){
					Method methodRead2 = p.getReadMethod();
					Object retVal = methodRead2.invoke(jbt2, null);
					System.out.println(retVal);
				}
			}
		    
		    /*拿到JavaBeanTest的所有方法,并调用方法名称为getAge的方法。*/
		    JavaBeanTest jbt3 = new JavaBeanTest();
		    BeanInfo bf2 = Introspector.getBeanInfo(JavaBeanTest.class);
		    MethodDescriptor[] md3 = bf1.getMethodDescriptors();
		    for (MethodDescriptor m : md3) {
				if(m.getName().equals("getAge")){
					Method methodRead3 = m.getMethod();
					Object retVal = methodRead3.invoke(jbt3, null);
					System.out.println(retVal);
				}
			}		    
         } catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}    
	}
}
输出结果:
22
没有名字
-1

学习心得:观看资料–》思考问题—》实践证明—》整理记录—》= 有思想的技术大牛!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值