注解的作用:
在不能给类中的属性set值时用到;
例如:
注解单个属性
package com.lingzhuo;
import java.lang.reflect.Field;
public class Student {
public Student() {
Class<Student> clazz=Student.class;
try {
Field field=clazz.getDeclaredField("name");
StudentAnnotation sa=field.getAnnotation(StudentAnnotation.class);
if(sa!=null){
String s=sa.value();
field.set(this, s);
}
} catch (NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@StudentAnnotation("张三")
private String name;
private int age;
public String getName() {
return name;
}
}
package com.lingzhuo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface StudentAnnotation {
String value();//只有一个注解时用value
//int age();
}
package com.lingzhuo;
public class Test1 {
public static void main(String[] args) {
Student student=new Student();
System.out.println(student.getName());
}
}
package com.lingzhuo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface StudentAnnotation {
int age();
}
package com.lingzhuo;
import java.lang.reflect.Field;
public class Student {
private String name;
@StudentAnnotation(age=18)
private int age;
public Student(){
Class clazz=Student.class;
try {
Field field=clazz.getDeclaredField("age");
StudentAnnotation sa=field.getAnnotation(StudentAnnotation.class);
if(sa!=null){
int i=sa.age();
field.setAccessible(true);
field.set(this, i);
field.setAccessible(false);
}
} catch (NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String clazz;
private String sex;
public String getClazz() {
return clazz;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getSex() {
return sex;
}
}
package com.lingzhuo;
public class Test3 {
public static void main(String[] args) {
Student zhangsan=new Student();
System.out.println(zhangsan.getAge());
}
}
运行得到:
18
同时注解多个属性
package com.lingzhuo.annotation;
public class Teacher {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.lingzhuo.annotation;
public class Teacher {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.lingzhuo.annotation;
import java.lang.reflect.Field;
public class Clazz {
@TeacherAnnotation(age=18,name="张三")
//注意 : 这的属性类型和注解类 的类名相同
private Teacher mathTeacher;
@TeacherAnnotation(age=18,name="张三")
private Teacher javaTeacher;
@TeacherAnnotation(age=18,name="张三")
private Teacher cTeacher;
public Teacher getMathTeacher() {
return mathTeacher;
}
public Teacher getJavaTeacher() {
return javaTeacher;
}
public Teacher getcTeacher() {
return cTeacher;
}
public Clazz(){
Class<Clazz> clazz = Clazz.class;
/*
* 得到clazz对象的所有属性并且把它放在Field类型的数组中 Field 提供有关类或接口的单个字段的信息,以及对它的动态访问权限
* Array 允许在执行 get 或 set 访问操作期间进行扩展转换,但如果将发生收缩转换,则抛出一个
* IllegalArgumentException。
*/
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {// 遍历数组
/*
* 如果存在该元素的指定类型的注释,则返回这些注释,否则返回 null。
*/
TeacherAnnotation ta = field.getAnnotation(TeacherAnnotation.class);
if (ta == null) {
continue;
} else {
int age = ta.age();// 得到注解的age的值如果没有则写默认值
String name = ta.name();// 得到注解的name的值
Teacher teacher = new Teacher();// 创建一个被注解的类的对象
teacher.setAge(age); // 根据注解的值设置新的对象
teacher.setName(name);
try {
field.setAccessible(true);// 设置为不检查属性的访问修饰符
field.set(this, teacher);// 将teacher赋值给clazz对象???this
field.setAccessible(false);// 设置检查属性的访问修饰符
} catch (IllegalArgumentException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package com.lingzhuo.annotation;
public class Test {
public static void main(String[] args) {
Clazz clazz=new Clazz();
System.out.println(clazz.getJavaTeacher().getName()+" " +clazz.getJavaTeacher().getAge());
}
}
运行得到:
张三 18