网站登录页面模板下载,西安做网站费用,军事最新消息今天,齐家网和土巴兔装修哪家好Java基础之注解 一、Java注解1.1、内置注解1.2、元注解1.3、自定义注解 一、Java注解
注解#xff08;Annotation#xff09;是一种为程序代码提供元数据#xff08;metadata#xff09;的方式。注解提供了关于程序代码的额外信息#xff0c;这些信息可以在运行时或编译时… 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();}}
}