什么是注解
是从JDK5.0开始引入的新技术。· Annotation的作用︰
-不是程序本身,可以对程序作出解释。(这一点,跟注释没什么区别)-可以被其他程序(比如∶编译器等)读取。(注解信息处理流程,是注解
和注释的重大区别。如果没有注解信息处理流程,则注解毫无意义). Annotation的格式︰
–注解是以“@注释名”在代码中存在的,还可以添加一些参数值,例
如:@SuppressWarnings(value=“unchecked”)。
. Annotation在哪里使用?
一可以附加在package, class, method, field等上面,相当于给它们添加
了额外的辅助信息,我们可以通过反射机制编程实现对这些元数据的访问。
内置注解
@Override
-定义在java.lang.Override中,此注释只适用于修辞方法,表示一个方法声明打算重写超类中的另一个方法声明。
例如:`
@Override
public String toString() {
return "";
}
//toString 方法是父类的一个方法,如果是
@Override
public String tostring() {
return "";
}
则编译器会报错,因为父类中没有tostring()这个方法
. @Deprecated
一定义在java.lang.deposated中,此注释可用于修辞方法、属性、类,
表示不鼓励程序员使用这样的元素,通常是因为它很危险或存在更好的选择。
例如:
@Deprecated
public static void test001() {
System.out.println("test001");
}
如果调用该方法,则test001()上会出现删除线,即:test001则表示不建议使用,但可以使用
. @SuppressWarnings
一定义在java.lang.Suppress Warnings中,用来抑制编译时的警告信息.
例如:
在该代码中出现警告而你想让警告消失则可以加入@SuppressWarnings,即:
自定义注解
元注解
在这里主要说前两个注解
自定义注解完整例子:
package com.lzy.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
@Target(value= {ElementType.METHOD,ElementType.TYPE})//表示该注解只能用于方法 类,接口,枚举
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtAnnotation01 {
String studentName() default "";
int age() default 0;
int id()default -1;
String[] schools()default{"清华大学","北京大学"};
}
package com.lzy.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
@Target(value= {ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtAnnotation02 {
String value();
}
package com.lzy.annotation;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Demo02 {
@SxtAnnotation01(age=19,studentName="老高",id=1001,schools= {"北京大学","三江学院"})
public void test() {
}
@SxtAnnotation02("aaaa")
public void test2() {
}
}
自定义注解SxtAnnotation01中,9行表示表示该注解只能用于方法 类,接口,枚举
12-16表示一些参数名和参数类型 default表示设置的默认值。Demo02()为实现类
现在自定义注解后。如果不通过反射等,则这些注解将毫无意义,下面为通过反射机制读取注解例子
自定义注解SxtTable
package com.lzy.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value= {ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtTable {
String value();//如果自定义注解只有一个参数,则设置为“value”
}
自定义注解SxtTable
package com.lzy.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value= {ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface SxtTable {
String value();
}
SxtStudent类
package com.lzy.annotation;
@SxtTable("tb_student")
public class SxtStudent {
@SxtField(columnName = "id", type = "int", length = 10)
private int id;
@SxtField(columnName = "sname", type = "varchar", length = 10)
private String studentName;
@SxtField(columnName = "age", type = "int", length = 3)
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Demo03类
package com.lzy.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class Demo03 {
public static void main(String[] args) {
try {
Class clazz=Class.forName("com.lzy.annotation.SxtStudent");
//获得类的所有有效注解
Annotation []annotations=clazz.getAnnotations();
for(Annotation a:annotations) {
System.out.println(a);
}
//获得类的指定的注解
SxtTable st=(SxtTable)clazz.getAnnotation(SxtTable.class);
System.out.println(st.value());
//获得类的属性的注解
Field f=clazz.getDeclaredField("studentName");
SxtField sxtField=f.getAnnotation(SxtField.class);
System.out.println(sxtField.columnName()+"--"+sxtField.type()+"--"+sxtField.length());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果
@com.lzy.annotation.SxtTable(“tb_student”)
tb_student
sname–varchar–10