注释是一种元数据形式,提供有关程序的信息,但不是程序本身的一部分。 Annotation 不会影响它们 Annotate 的代码的操作。
现在让我们来看看目前列出的不同类型的Java注释:
- 预定义注释。:@Deprecated、@Override、@SuppressWarnings、@SafeVarargs、@FunctionalInterface。
- 元注释:@Retention、@Documented、@Target、@Inherited、@Repeatable。
- 自定义注释:这些由用户定义。 (我们将学习在本模块中创建自定义注释)。
极客,现在你一定想知道我们如何创建我们自己的Java注释,因为它是指顺序简单的步骤如下:
- 要创建您自己的Java Annotation,您必须使用@interface Annotation_name,这将为您创建一个新的Java Annotation。
- @interface将描述新的注解类型声明。
- 在为您的注解命名后,您将需要创建一个语句块,您可以在其中声明一些变量。
现在继续在Java可以定义三种形式的注释,如下所示:
- 标记注释:这些注释中没有声明或定义变量。
- 单值注解:这些注解中只声明或定义了一个变量。
- 多值注释:这些注释可以在其中声明和定义多种类型的多个变量。
执行:
让我们以一个名为books_data的自定义注解为例来了解如何声明不同形式的注解。
@interface books_data //using the syntax : @interface Annotation_name, we declared a new annotation here.
{ //beginning of annotation declaration and defination
/*
defining variables inside an annotation is optional.
The number of variables declared inside an annotation will describe its form.
*/
} //end of annotation declaration and defination
示例 1:
Java
// Java Programwhere Illustrating Declaration of
// Custom Marker Annotation
// Importing I/O classes
import java.io.*;
// Sample for marker Annotation:
// Custom annotation declaration
@interface books_data
{
// No variable declared here
}
// Main class
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println(
"example of Marker Annotations.");
}
}
Java
// Java Program Illustrating Declaration of
// Custom Single Value Annotation
// Importing input output classes
import java.io.*;
// Sample for single value Annotation:
// Custom annotation declaration
@interface books_data
{
// Single variable declaration
String book_name();
}
// Main class
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println(
"example of single value Annotation.");
}
}
Java
// Java Programwhere Illustrating Declaration of
// Multi value Annotation
// Importing input output classes
import java.io.*;
// Sample for multi value annotation:
// Custom annotation declaration
@interface books_data
{
// Multiple variable declarations
String book_name();
int book_price();
String author();
}
// Main class
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println(
"example of multi value Annotation.");
}
}
Java
// Java Program illustrating Use of Custom Annotation
// Importing input output classes
import java.io.*;
// Sample for marker annotation:
// Custom annotation declaration
@interface books_data
{
// Multiple variable declaration
String book_name();
int book_price();
String author();
}
// Using the custom Annotation
@books_data(book_name = "Effective Java", book_price = 30,
author = "Joshua Bloch")
// Class 1
class book_store {
}
// Class 2
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println("how to use the annotations");
}
}
Java
// Java Program Illustrating Default Values Declaration
// of Variables Inside an Annotation
// Importing input output classes
import java.io.*;
// Sample for Marker annotation
@interface books_data
{
// Custom annotation declaration
String book_name() default "Effective Java";
// Declaring the default values
int book_price() default 30;
String author() default "Joshua Bloch";
// Multiple variable declaration
}
// Using the custom Annotation
@books_data
// Class 1
class book_store {
}
// Class 2
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println(
"Annotation using default values");
}
}
example of Marker Annotations.
Since no variable is declared inside this annotation, this will be known as Marker Annotation.
示例 2:
Java
// Java Program Illustrating Declaration of
// Custom Single Value Annotation
// Importing input output classes
import java.io.*;
// Sample for single value Annotation:
// Custom annotation declaration
@interface books_data
{
// Single variable declaration
String book_name();
}
// Main class
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println(
"example of single value Annotation.");
}
}
example of single value Annotation.
We can see that we have declared a variable book_name of type String, and it’s the only variable declared inside the Annotation, hence, this is an example of Single-Value Annotation.
示例 3:我们将在注释中声明以下变量:
@interface books_data {
String book_name();
int book_price();
String author();
}
Java
// Java Programwhere Illustrating Declaration of
// Multi value Annotation
// Importing input output classes
import java.io.*;
// Sample for multi value annotation:
// Custom annotation declaration
@interface books_data
{
// Multiple variable declarations
String book_name();
int book_price();
String author();
}
// Main class
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println(
"example of multi value Annotation.");
}
}
example of multi value Annotation.
We have declared multiple variables inside our Annotation, this is an example of Multi-value Annotation.
现在让我们看看如何使用自定义注解 让我们看看如何使用自定义注释:
- 方法一:默认注解
- 方法二:自定义注解
专注于自定义注释,为了使用您的自定义注释,我们只需要使用您的注释名称调用您的注释,前面带有@symbol并以有序的方式将声明的变量的值传递给您在注解。
示例 1:
Java
// Java Program illustrating Use of Custom Annotation
// Importing input output classes
import java.io.*;
// Sample for marker annotation:
// Custom annotation declaration
@interface books_data
{
// Multiple variable declaration
String book_name();
int book_price();
String author();
}
// Using the custom Annotation
@books_data(book_name = "Effective Java", book_price = 30,
author = "Joshua Bloch")
// Class 1
class book_store {
}
// Class 2
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println("how to use the annotations");
}
}
how to use the annotations
However, if you don’t want to specify values when you are using the Annotation you can initialize the values inside your Annotation using the default value.
所以最后让我们讨论它是如何完成的
Note: Using default value is optional.
示例 2:
Java
// Java Program Illustrating Default Values Declaration
// of Variables Inside an Annotation
// Importing input output classes
import java.io.*;
// Sample for Marker annotation
@interface books_data
{
// Custom annotation declaration
String book_name() default "Effective Java";
// Declaring the default values
int book_price() default 30;
String author() default "Joshua Bloch";
// Multiple variable declaration
}
// Using the custom Annotation
@books_data
// Class 1
class book_store {
}
// Class 2
class books {
// Main driver method
public static void main(String[] args)
{
// Print statement
System.out.println(
"Annotation using default values");
}
}
Annotation using default values
If you still initialize the values when you are using your annotation when you have declared the default values, the initialized value will overwrite the default values.