关键字IntroSpector-->JavaBean--->特殊的java类 名字符合某种规则
int getAge()
void setAge(int age)
属性根据方法来
class Person{
int x,
public int getAge(){
return x;
}
public setAge(int age){
this.x = age;
}
}
Age--->如果第二个字母是小写,则把第一个字母小写--》age
gettime --> time
setTime---> time
getCPU -----> CPU
ReflectPoint1 pt1 = new ReflectPoint1(3, 5);
String propertyName = "x";
//x---->X--->getX()---->MethodGetX();
//属性描述 pt1.getClass()当做JavaBean , propertyName 属性 。相当于一个类,一个属性
//就容易得到属性的get set 方法。
private static Object getProperty(Object pt1, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
/* PropertyDescriptor pt = new PropertyDescriptor(propertyName, pt1.getClass());
//获得方法
Method methodGetX = pt.getReadMethod();
//传递对象pt1
Object retVal = methodGetX.invoke(pt1);*/
BeanInfo beanInfo = Introspector.getBeanInfo(pt1.getClass());
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
Object retVal = null;
for(PropertyDescriptor pd:pds){
if(pd.getName().equals(propertyName)){
Method methodGetX = pd.getReadMethod();
//传递对象pt1
retVal = methodGetX.invoke(pt1);
break;
}
}
private static void setProperties(Object pt1, String propertyName,
Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
PropertyDescriptor pt2 = new PropertyDescriptor(propertyName, pt1.getClass());
//获得方法
Method methodSetX = pt2.getWriteMethod();
//传递对象pt1
methodSetX.invoke(pt1,value);
}
两个重要工具包的导入
BeanUtils logging
新建lib 把jar包考入。
add buildPath
getproperty(pt1,"x") 参数为字符串类型
BeanUtils.getProperty(pt1, propertyName)
setproperty(pt1, "x", 9 )
BeanUtils.setProperty(pt1, "x", "11");
PropertyUtils.setProperty(pt1, "x", 100);// 此处不是字符串
记住两个方法:
set()
get()
注解:
关键字Annotation
一个注解是一个类,加上一个注解,就代表创建了一个实例对象,注解的创建方式基本等同于@j接口
(1) @ suppresswarnings(" depracation" ); 压缩警告,不在提醒过时的方法,命令行中效果明显。
(2) @Despracated方法过时
(3)@Override覆盖。
Overload是重载,Override是覆盖的,也就是重写。
注解就相当于一个标记,加了注解,就等于为程序打了标记,没加则等于没加标记,以后,Java编译器开发工具和其他程序可以用反射来了解类及各种元素上有无任何标记,看有什么样的标记,就去干相应的事。
标记可以加载包,类,字段,方法,方法的参数以及局部变量上。
应用注解:
注解类: 应用注解类: 对“应用了注解类的类”进行反射操作的类
@interface A{ @A Class C{
} class B{ B.class.isAnnotionPerson(A.class)
} A a = B.class.getAnnotion(A.class);
@Retention Enum RetentionPolicy
class @override source
source @suppresswarnings source
runtime @depracated runtime
package cn.itcast.day2;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//注解的注解 元注解
//注解保留的运行时期,
//注解的生命周期有三个阶段:SOURCE,CLASS(默认),RUNTIME
import cn.itcast.day1.EnumTest;
@Retention(RetentionPolicy.RUNTIME)
//注解的作用范围
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface ItcastAnnotation {
String color() default "red";//设置默认值
String value();
int[] arrs();
EnumTest.TrafficLamp lamp() default EnumTest.TrafficLamp.RED;
MetaAnnotation annotationArr() default @MetaAnnotation("hhs") ;
}
package cn.itcast.day2;
import javax.jws.soap.InitParam;
@SuppressWarnings("deprecation")
@ItcastAnnotation(annotationArr=@MetaAnnotation("md"),
color="yellow",value="efg",arrs={1,2,3})
public class AnnotationTest {
//一个注解是一个类,此处是创建了一个实例。
@SuppressWarnings("deprecation")
//@ItcastAnnotation("xyz")
public static void main(String[] args) throws Exception {
System.runFinalizersOnExit(true);
判断是否含有注解
if(AnnotationTest.class.isAnnotationPresent(ItcastAnnotation.class)){
得到注解
ItcastAnnotation annotation = (ItcastAnnotation)AnnotationTest.class.getAnnotation(ItcastAnnotation.class);
获取注解的属性
System.out.println(annotation.color());
System.out.println(annotation.arrs().length);
System.out.println( annotation.value());
System.out.println( annotation.lamp().nextLamp());
System.out.println( annotation.annotationArr().value());
}
}
---------------------- android培训、java培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net/heima