Home  >  Article  >  Java  >  Application of interfaces and abstract classes in Java annotations

Application of interfaces and abstract classes in Java annotations

PHPz
PHPzOriginal
2024-05-02 11:48:011129browse

Annotations can be defined in Java through interfaces or abstract classes to provide metadata for classes, methods, or fields. Interface as annotation type: implement java.lang.annotation.Annotation interface, such as: @MyAnnotation("Hello, world!") Abstract class as annotation type: extend java.lang.annotation.Annotation abstract class, such as: @MyAnnotation(value "Hello, world!") Practical case: Use annotations to verify method parameters, for example: @NotNull, to check whether the parameters are non-null, otherwise an exception will be thrown.

接口和抽象类在 Java 注解中的应用

Interfaces and abstract classes in annotations

Annotations are used in Java to provide relevant classes, methods and fields to the compiler metadata. Interfaces and abstract classes can be used as annotation types, allowing you to define specific constraints on annotations.

Interface as annotation type

Interface can be used as annotation type by implementing the java.lang.annotation.Annotation interface. For example:

public @interface MyAnnotation {
    String value();
}

Use this annotation:

@MyAnnotation("Hello, world!")
public class MyClass {}

Abstract class as annotation type

Abstract class can also be used as annotation type, by extending java.lang.annotation.Annotation Abstract class. For example:

public abstract @interface MyAnnotation {
    String value();
}

Use this annotation:

@MyAnnotation(value="Hello, world!")
public class MyClass {}

Practical case

In the following practical case, we will use annotations to verify method parameters:

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.PARAMETER)
public @interface NotNull {
}

public class MyClass {

    public void myMethod(@NotNull String param) {
        // 验证参数 non-null
        if (param == null) {
            throw new IllegalArgumentException("参数不能为空!");
        }
        // 使用参数...
    }

}

Use this annotation:

public class Client {

    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        myClass.myMethod("Hello, world!");
    }

}

Running this code will throw IllegalArgumentException because the myMethod method's parameter does not provide a non-null value.

The above is the detailed content of Application of interfaces and abstract classes in Java annotations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn