黑马程序员_基础加强3

---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IO开发S</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------

        一. 内省(Introspector)

       1.   对JavaBean进行操作,它是一个特殊的java类.JavaBean的属性是根据方法名称而来的,如setAge,则代表有一个Age属性.

       2.  属性的命名规则:

          (1)去掉set或get的开头

          (2)检查去掉开头后的第二个字母如果是小写,则将第一个字母改成小写,大写则不用改

       用内省的方式得到get,set方法,例:

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class IntroSpectorDemo {

/**
* @param args
* @throws Exception 

* 用内省的方式获得get.set方法
*/
public static void main(String[] args) throws Exception {
/*ReflectPoint reflectPoint = new ReflectPoint(2, 3);
String str = "x";
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(str, reflectPoint.getClass());
Method method = propertyDescriptor.getWriteMethod();  //获取set方法
method.invoke(reflectPoint, 4);  //调用set方法
System.out.println(reflectPoint.getX());*/

/*String propertyNamey = "y";
PropertyDescriptor pro = new PropertyDescriptor(propertyNamey, reflectPoint.getClass());
Method getMethod = pro.getReadMethod();
Object obj = getMethod.invoke(reflectPoint);
System.out.println(obj);


String propertyName = "x";
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(propertyName, reflectPoint.getClass());
Method method = propertyDescriptor.getReadMethod();  //获取get方法
Object retVal = method.invoke(reflectPoint);
System.out.println(retVal);*/

//通过beanInfo找get方法
ReflectPoint reflectPoint = new ReflectPoint(2, 3);
String propertyName = "x";
beanInfo(reflectPoint,propertyName);

}


public static Object beanInfo(Object obj,String propertyName) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {

BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); //把一般类当成javaBean来看,获取信息
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); //得到所有属性的描述
Object retVal = null;
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { //遍历数组,找到所有属性
if (propertyDescriptor.equals(propertyName)) {  //判断是否有需要的属性
Method method = propertyDescriptor.getReadMethod();  //获取get方法
retVal = method.invoke(obj);  //调用方法
break;
}
}

return retVal;
}


}


       二.BeanUtils工具包:

        1. 它是专门针对JavaBean操作的工具类,APaChe提供.注意,用这个工具包get得到的属性类型是String,因此在set值时传入的也是Stirng类型,因为在web开发中,传给服务器的都是字符串.

         注意:JDK1.7的新特性,可以设置Map的值,如:

         Map map = {name:"张三",age:23}

        Beaunutils.setProperty(map,"name","李四");

        2.有三种取值,设值方式

         (1)手动设置

         (2)BeanUtils工具包

         (3)PropertyUtils类,注意,与BeanUtils不同的是,它是以属性本身的类型进行操作

          

          三:注解

         1.文件注解的生命周期有三个阶段,分别是:

         (1) java源文件阶段,  RetetionPolicy.SOURCE

         (2)class文件阶段     Pentention.CLASS

         (3)内存中的字节码阶段,   默认值是这个阶段

          2.为注解增加属性:注解的属性相当于接口中的方法,具体写时前面省略了public,例:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)//元注解
public @interface ItcastAnnotation {
String color() default "blue"; 
String value();
int[] arrAttr() default{3,4,5};
MetaAnnotation ma() default @MetaAnnotation("qqq");
}


@ItcastAnnotation(color = "red",value = "abc",arrAttr={1,2,3},ma=@MetaAnnotation("aaa"))
public class AnnotationTest {


/**
* @param args
*/
@ItcastAnnotation("abc")
public static void main(String[] args) {
if(AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)){
ItcastAnnotation itcastAnnotation = (ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class);
System.out.println(itcastAnnotation.color());
System.out.println(itcastAnnotation.value());
System.out.println(itcastAnnotation.arrAttr().length);
System.out.println(itcastAnnotation.ma().value());
}

}

}

         注意:属性的默认值及value属性的特殊应用

  ---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IO开发S</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------        

         


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值