Java基础之注解
- 一、Java注解
- 1.1、内置注解
- 1.2、元注解
- 1.3、自定义注解
一、Java注解
注解(Annotation)是一种为程序代码提供元数据(metadata)的方式。注解提供了关于程序代码的额外信息,这些信息可以在运行时或编译时被读取并处理。Java 的注解以 @
符号开头,例如 @Override
、@Deprecated
等。
1.1、内置注解
@Override
: 表示该方法覆盖了父类的方法。通常用于帮助编译器检查是否正确地覆盖了父类的方法。
@Deprecated
: 表示该方法或类已经过时,不推荐使用。编译器在使用过时的方法或类时会发出警告。
@SuppressWarnings
: 抑制编译器警告。可以用于特定的方法或整个类。
class Parent {public void printMessage() {System.out.println("Parent class");}
}class Child extends Parent {@Overridepublic void printMessage() {System.out.println("Child class");}
}class DeprecatedExample {@Deprecatedpublic void oldMethod() {System.out.println("This method is deprecated.");}
}class SuppressWarningExample {@SuppressWarnings("unchecked")public void uncheckedOperation() {// 一些操作}
}public class AnnotationExample {public static void main(String[] args) {// 使用 @Override 注解Parent parent = new Child();parent.printMessage(); // 输出 "Child class"// 使用 @Deprecated 注解DeprecatedExample example = new DeprecatedExample();example.oldMethod(); // 输出 "This method is deprecated."// 使用 @SuppressWarnings 注解SuppressWarningExample suppressWarningExample = new SuppressWarningExample();suppressWarningExample.uncheckedOperation(); // 不会产生未经检查的警告}
}
1.2、元注解
元注解是应用于其他注解的注解,用于控制注解的行为。
@Retention
: 指定注解的保留策略,即注解在何时可用。有RetentionPolicy.SOURCE
、RetentionPolicy.CLASS
和RetentionPolicy.RUNTIME
三种策略。@Target
: 指定注解可以应用于的元素类型,如类、方法、字段等。@Documented
: 表示注解将包含在 JavaDoc 文档中。
import java.lang.annotation.*;@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
@Documented
@Inherited
public @interface MyCustomAnnotation {String value() default "default value";int count() default 0;
}@MyCustomAnnotation(value = "Custom Value", count = 42)
class AnnotatedClass {@MyCustomAnnotation(value = "Custom Method")public void annotatedMethod() {// 方法实现}
}public class AnnotationIntegrationExample {public static void main(String[] args) {// 获取类级别的注解信息MyCustomAnnotation classAnnotation = AnnotatedClass.class.getAnnotation(MyCustomAnnotation.class);System.out.println("Class Annotation Value: " + classAnnotation.value());System.out.println("Class Annotation Count: " + classAnnotation.count());// 获取方法级别的注解信息try {AnnotatedClass annotatedInstance = new AnnotatedClass();MyCustomAnnotation methodAnnotation = annotatedInstance.getClass().getMethod("annotatedMethod").getAnnotation(MyCustomAnnotation.class);System.out.println("Method Annotation Value: " + methodAnnotation.value());System.out.println("Method Annotation Count: " + methodAnnotation.count());} catch (NoSuchMethodException e) {e.printStackTrace();}}
}
1.3、自定义注解
自定义注解的定义方式类似于接口,但使用 @interface
关键字。
- @ interface用来声明一个注解,格式:public@ interface注解名{定义内容}
- 其中的每一个方法实际上是声明了一个配置参数
- 方法的名称就是参数的名称
- 返回值类型就是参数的类型(返回值只能是基本类型, Class, String,enum)
- 可以通过 defau来声明参数的默认值
- 如果只有一个参数成员,一般参数名为vaue
- 注解元素必须要有值,我们定义注解元素时,经常使用空字符串,0作为默认值
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.TYPE, ElementType.METHOD})
public @interface MyCustomAnnotation {String value() default "default value";int count() default 0;
}// 使用自定义注解
@MyCustomAnnotation(value = "Custom Class", count = 10)
public class AnnotatedClass {@MyCustomAnnotation(value = "Custom Method", count = 5)public void annotatedMethod() {// 方法实现}
}public class CustomAnnotationExample {public static void main(String[] args) {// 获取类级别的注解信息MyCustomAnnotation classAnnotation = AnnotatedClass.class.getAnnotation(MyCustomAnnotation.class);System.out.println("Class Annotation Value: " + classAnnotation.value());System.out.println("Class Annotation Count: " + classAnnotation.count());// 获取方法级别的注解信息try {AnnotatedClass annotatedInstance = new AnnotatedClass();MyCustomAnnotation methodAnnotation = annotatedInstance.getClass().getMethod("annotatedMethod").getAnnotation(MyCustomAnnotation.class);System.out.println("Method Annotation Value: " + methodAnnotation.value());System.out.println("Method Annotation Count: " + methodAnnotation.count());} catch (NoSuchMethodException e) {e.printStackTrace();}}
}