复习巩固一下
定义注解:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* define annotation
* @author ffr
*
*/
@Target(ElementType.TYPE)//指定该注解的应用域,只能对类进行注解
@Retention(RetentionPolicy.RUNTIME)//指定该注解的级别(SOURCE/CLASS/RUNTIME),RUNTIME运行时可以使用
public @interface DBTable {
public String name();//自定义的注解属性
}
使用注解:
@DBTable(name="setting")
public class Setting implements Serializable {
private int id;
private String version;
private String apkUrl;
private String content;
//省略......
}
解析,获取注解值:
import cn.kepu.banche.common.DBTable;
import cn.kepu.banche.model.Setting;
public class TestAnno {
public static void main(String[] args) throws Exception{
DBTable db = (DBTable) Setting.class.getAnnotation(DBTable.class);
if(db != null){
System.out.println(db.name());
}else{
System.out.println("no annotation");
}
}
}