重生之我在deepseek中学习java 第十四天之注解(Annotation)-Java-E先生的博客
Java
MySQL
大数据
Python
前端
黑科技
大语言模型
    首页 >> 互联网 >> Java

重生之我在deepseek中学习java 第十四天之注解(Annotation)

[导读]:Java学习指南-第十四天:注解(Annotation) 1.注解简介 注解(Annotation)是Java5引入的一种元数据机制,用于为代码提供额外的信息。注解可以用于类、方法、字段、参数等元素上,编译器、工具和框架可以通过反射读取注解信息并进行相应的处理。 2.内置注解 Java提供...

Java学习指南-第十四天:注解(Annotation)

    1.注解简介

    注解(Annotation)是Java5引入的一种元数据机制,用于为代码提供额外的信息。注解可以用于类、方法、字段、参数等元素上,编译器、工具和框架可以通过反射读取注解信息并进行相应的处理。

    2.内置注解

    Java提供了一些内置注解,常用的有:

  1. @Override:表示方法重写父类的方法。

  2. @Deprecated:表示方法或类已过时,不推荐使用。

  3. @SuppressWarnings:抑制编译器警告。

    2.1使用内置注解

class Parent {
    void display() {
        System.out.println("Parent display");
    }
}

class Child extends Parent {
    @Override
    void display() {
        System.out.println("Child display");
    }

    @Deprecated
    void oldMethod() {
        System.out.println("This method is deprecated");
    }

    @SuppressWarnings("unchecked")
    void uncheckedWarning() {
        java.util.List list = new java.util.ArrayList();
        list.add("Hello");
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        child.display();
        child.oldMethod();
        child.uncheckedWarning();
    }
}

    3.自定义注解

    Java允许用户自定义注解。自定义注解使用@interface关键字定义,可以包含元素(类似于方法)。

    3.1定义注解

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.METHOD)
public @interface MyAnnotation {
    String value() default "default value";
    int number() default 0;
}

    3.2使用注解

public class Main {
    @MyAnnotation(value = "custom value", number = 10)
    public void myMethod() {
        System.out.println("Executing myMethod");
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.myMethod();
    }
}

    4.注解处理器

    通过反射机制,可以在运行时读取注解信息并进行相应的处理。

    4.1读取注解信息

import java.lang.reflect.Method;

public class Main {
    @MyAnnotation(value = "custom value", number = 10)
    public void myMethod() {
        System.out.println("Executing myMethod");
    }

    public static void main(String[] args) {
        try {
            Main main = new Main();
            Method method = main.getClass().getMethod("myMethod");

            // 读取注解信息
            MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
            if (annotation != null) {
                System.out.println("Value: " + annotation.value());
                System.out.println("Number: " + annotation.number());
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}

    5.元注解

    元注解是用于注解其他注解的注解。Java提供了以下几种元注解:

  • @Retention:指定注解的保留策略(SOURCE、CLASS、RUNTIME)。

  • @Target:指定注解可以应用的目标元素(TYPE、METHOD、FIELD等)。

  • @Documented:指定注解是否包含在JavaDoc中。

  • @Inherited:指定注解是否可以被继承。

    5.1使用元注解

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.METHOD)
public @interface MyAnnotation {
    String value() default "default value";
    int number() default 0;
}

image.png

    7.示例代码

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface TestAnnotation {
    String name();
    int priority() default 1;
}

public class Main {
    @TestAnnotation(name = "testMethod", priority = 2)
    public void testMethod() {
        System.out.println("Executing testMethod");
    }

    @SuppressWarnings("unchecked")
    public void uncheckedWarning() {
        java.util.List list = new java.util.ArrayList();
        list.add("Hello");
    }

    public static void main(String[] args) {
        try {
            Main main = new Main();
            Method method = main.getClass().getMethod("testMethod");

            // 读取注解信息
            TestAnnotation annotation = method.getAnnotation(TestAnnotation.class);
            if (annotation != null) {
                System.out.println("Name: " + annotation.name());
                System.out.println("Priority: " + annotation.priority());
            }

            main.uncheckedWarning();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}

    8.总结

    今天的内容涵盖了Java注解的基本概念和常用技术,包括内置注解、自定义注解、注解处理器和元注解。注解是Java编程中非常强大的工具,务必熟练掌握。明天我们将深入学习Java的Lambda表达式和函数式编程。

    提示:多动手编写代码,尝试使用注解解决实际问题,理解注解的工作原理和适用场景。

    祝你学习愉快!


本文来自E先生的博客,如若转载,请注明出处:https://javajz.cn

留言区

联系人:
手   机:
内   容:
验证码:

历史留言

欢迎加Easy的QQ