进阶day12-元注解
This commit is contained in:
15
javaSE-day12/src/com/inmind/Demo08.java
Normal file
15
javaSE-day12/src/com/inmind/Demo08.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.inmind;
|
||||
/*
|
||||
元注解:用来修饰注解的注解
|
||||
@Target:设置某一个注解的作用范围
|
||||
ElementType.TYPE 类
|
||||
ElementType.FIELD 属性
|
||||
ElementType.CONSTRUCTOR 构造器
|
||||
ElementType.METHOD 方法
|
||||
@Retention:设置某一个注解的生命周期
|
||||
SOURCE:编译时期有效
|
||||
CLASS:编译后,运行前有效
|
||||
RUNTIME:运行时有效 (比较常用,结合反射操作)
|
||||
*/
|
||||
public class Demo08 {
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.inmind.annotation03;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/*
|
||||
@@ -18,6 +20,7 @@ import java.util.ArrayList;
|
||||
|
||||
6.注解在使用时,属性值必须赋值,默认值可以覆盖
|
||||
*/
|
||||
@Target(value = {ElementType.TYPE,ElementType.FIELD,ElementType.METHOD,ElementType.CONSTRUCTOR})
|
||||
public @interface MyAnnotation {
|
||||
public static final int age = 1;//注解中的常量
|
||||
String name() default "张三";//name属性
|
||||
|
||||
@@ -25,7 +25,7 @@ public class Student {
|
||||
}
|
||||
|
||||
@MyAnnotation(1)
|
||||
private int show(@MyAnnotation(1) int i){
|
||||
private int show(int i){
|
||||
System.out.println("有参有返回值的show方法执行了");
|
||||
return i+100;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user