---------------------- android培训、java培训、期待与您交流! ----------------------
内省
25. 对JavaBean的简单内省操作
package cn.xy.day1;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class IntroSpectorTest {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
JavaBeanX jbx = new JavaBeanX(3,5);
String propertyName = "x";
Object retVal = getProperty(jbx, propertyName);
System.out.println(retVal);
Object value = 7;
setProperty(jbx, propertyName, value);
System.out.println(jbx.getX());
}
private static void setProperty(Object jbx, String propertyName,
Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd2 = new PropertyDescriptor(propertyName,jbx.getClass());
Method methodSetX = pd2.getWriteMethod();
methodSetX.invoke(jbx,value);
}
private static Object getProperty(Object jbx, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName,jbx.getClass());
Method methodGetX = pd.getReadMethod();
Object retVal = methodGetX.invoke(jbx);
return retVal;
}
}
26.应用BeanUtils工具包来取得和设置javabean属性的值:
BeanUtils.getProperty(className,propertyName);
BeanUtils.setProperty(className,propertyName,propertyValue);
注解
27.注解相当一种标记,在程序中加了注解就等于为程序打上了某种标记,没加,则等于没有某种标记,以后,javac编译器,开发工具和其他程序可以用反射来了解你的类及各种元素上有无何种标记,看你有什么标记,就去干相应的事。标记可以加在包,类,字段,方法,方法的参数以及局部变量上。
28.@Retention元注解,其三种取值:RetetionPolicy.SOURCE、 RetetionPolicy.CLASS 、RetetionPolicy.RUNTIME分别对应:java源文件àclass文件à内存中的字节码。默认是RetetionPolicy.CLASS。
29.注解应用实例:
package cn.xy.day2;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME) //元注解
public @interface XyAnnotation {
String color() default "blue";
String value();
int[] arrayAttr();
}
package cn.xy.day2;
@XyAnnotation(color="red",value="abc",arrayAttr={1,2,3})
public class AnnotationTest {
@SuppressWarnings("deprecation") //压缩警告,一个注解就是一个类,
public static void main(String[] args) {
// TODO Auto-generated method stub
System.
runFinalizersOnExit(true);AnnotationTest.
sayHello();if(AnnotationTest.class.isAnnotationPresent(XyAnnotation.class)){
XyAnnotation annotation = AnnotationTest.class.getAnnotation(XyAnnotation.class);
System.out.println(annotation.color());
System.out.println(annotation.value());
System.out.println(annotation.arrayAttr().length);
}
}
@Deprecated //过时
public static void
sayHello(){System.out.println("hi,xy");
}
}
泛型
31.泛型是提供给javac编译器使用的,可以限定集合中的输入类型,让编译器挡住源程序中的非法输入,编译器编译带类型说明的集合时会去除掉“类型”信息,使程序运行效率不受影响,对于参数化的泛型类型,getClass()方法的返回值和原始类型完全一样。由于编译生成的字节码会去掉泛型的类型信息,只要能跳过编译器,就可以往某个泛型集合中加入其他类型的数据。例如,用反射得到集合,在调用起add方法即可。
32. (1)ArrayLis<E>泛型类型,
(2)E称为类型变量或类型参数
(3)整个ArrayLis<Integer>称为参数化的类型
(4)Integer称为类型参数的实例或实际类型参数
(5)<>念着typeof
(6)ArrayLis称为原始类型
33.参数化类型与原始类型的兼容性
(1)参数化类型可以引用一个原始类型的对象,编译报告警告,例如,
Collection<String> c = new Vector();//可以
(2)原始类型可以引用一个参数化类型的对象,编译报告警告,例如,
Collection c = new Vector<String> ();//可以
34.参数化类型不考虑类型参数的继承关系:
Vector<String> v = new Vector<Object>(); //错误
Vector<Object> v = new Vector<String>(); //错误
35.在创建数组实例时,数组的元素不能使用参数化的类型,例如,下面语句有错误:
Vector<Integer> vectorList[] = new Vector<Integer>[10]; //错误
36.Vector v1 = new Vector<String>(); //可以
Vector<Object> v = v1; //可以
37.使用泛型中的?通配符可以引用其他各种参数化的类型,?通配符定义的变量主要用作引用,可以调用与参数无关的方法,不能调用与参数化有关的方法。
实例:定义一个方法,该方法用于打印出任意参数化类型的集合中的所有数据:
public static void printCollection(Collection<?> cols){
for(Object obj:cols){
System.out.println(obj);
}
//cols.add("String"); //错误,因为它不知自己未来匹配的就是String
cols.size(); //没错,此方法与类型参数没有关系
cols = new HashSet<Date>();
}
38.限定通配符的上边界:
正确:Vector<? extends Number> x = new Vector<Integer>();
错误:Vector<? extends Number> x = new Vector<String>();
39.限定通配符的下边界:
正确:Vector<? super Integer> x = new Vector< Number>();
错误:Vector<? super Integer> x = new Vector<Byte>();
限定通配符总是包括自己。
40.泛型的简单实例:
HashMap<String,Integer> maps = new HashMap<String,Integer>();
maps.put("xy", 22);
maps.put("xy1", 21);
maps.put("xy2", 20);
Set<Map.Entry<String,Integer>> entrySet = maps.entrySet();
for(Map.Entry<String,Integer> entry : entrySet){
System.out.println(entry.getKey() + ":" + entry.getValue());
}
---------------------- android培训、java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima