📜  Java的继承注解

📅  最后修改于: 2022-05-13 01:54:41.338000             🧑  作者: Mango

Java的继承注解

Java的注解有助于将元数据与程序元素(例如类、实例变量、方法等)相关联。注解还可用于将元数据附加到其他注解。这些类型的注释称为元注释。默认情况下, Java不允许继承自定义注释。 @inherited 是一种元注释,用于注释自定义注释,以便子类可以继承这些自定义注释。下面提到了使用@inherited 注释的语法。

@Inherited
@interface CustomAnnotation {
    String value () default "GFG";
}

场景:

下面我们将讨论这两种情况:

  • 案例一:使用@Inherited注解
  • 案例2:不使用@inherited Annotation

现在让我们讨论这两个场景并实现与Java程序相同的场景,以更好地理解它们。

案例一:使用@Inherited注解



在下面显示的代码示例中,我们创建了一个名为 CustomAnnotation 的自定义注释,并使用 @Inherited 注释对其进行了注释。我们使用 CustomAnnotation 来注释由 InheritedAnnotationDemo 类继承的 Super 类。 InheritedAnnotationDemo 还继承了 CustomAnnotation,因为它是使用 @Inherited 注释进行注释的。

例子

Java
// Java Program to Illustrating Use of Custom Annotations
// With @inherited annotation
 
// Importing required classes from java.lang package
import java.lang.annotation.*;
import java.lang.reflect.AnnotatedElement;
 
// Creating our single valued custom annotation
// with @inherited annotation
@Inherited
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
 
// Custom annotation
@interface CustomAnnotation
{
    String value() default "GFG";
}
 
// Annotating the super class using
// the custom annotation
@CustomAnnotation(value = "Sky is limitless")
 
// Class 1
// Parent
class Super {
}
 
// Class 2
// Child class
// This is our derived class which inherits the
// the Super class and the custom annotation
public class InheritedAnnotationDemo extends Super {
 
    // Method 1
    // Main driver method
    public static void main(String[] arg) throws Exception
    {
 
        // Printing the annotation used to annotated the
        // Super and InheritedAnnotationDemo classes
        System.out.println(
            new InheritedAnnotationDemo()
                .getClass()
                .getAnnotation(CustomAnnotation.class));
 
        System.out.println(
            new Super().getClass().getAnnotation(
                CustomAnnotation.class));
 
        // Obtaining the class class name and
        // printing the annotation info about the
        //  annotation info attached to the Super class
        Class obj = Super.class;
 
        // Calling the method 2 to
        // print the annotation states
        printAnnotationState(obj);
    }
 
    // Method 2
    // To print the annotation state
    static void printAnnotationState(AnnotatedElement ann)
    {
 
        // Obtaining all the annotations attached to the
        // passed element and storing it in an array
        Annotation[] annotationsArray
            = ann.getAnnotations();
 
        // Iterating on all the annotations stored inside of
        // the array above and printing their information
        for (Annotation annotation : annotationsArray) {
 
            // Print and display nameand value of annotation
            System.out.println(
                "Name of the annotation : "
                + annotation.annotationType());
            System.out.println(
                "Value : "
                + ((CustomAnnotation)annotation).value());
        }
    }
}


Java
// Java Program to Illustrating Use of Custom Annotations
// Without @inherited annotation
 
// Importing required classes from java.lang package
import java.lang.annotation.*;
import java.lang.reflect.AnnotatedElement;
 
// Creating our single valued custom
// annotation without @inherited annotation
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
// Custom annotation
@interface CustomAnnotation
{
    String value() default "GFG";
}
 
// Annotating the super class using our
// custom annotation
@CustomAnnotation(value = "Sky is limitless")
 
// Class 1
class Super {
}
 
// Class 2
// Main class
// This is our derived class which inherits the
// the Super class but does not inherit the
// CustomAnnotation
public class InheritedAnnotationDemo extends Super {
    public static void main(String[] arg) throws Exception
    {
 
        // Printing the annotation used to annotated the
        // Super and InheritedAnnotationDemo classes
        System.out.println(
            new InheritedAnnotationDemo()
                .getClass()
                .getAnnotation(CustomAnnotation.class));
 
        // As we haven't used the @Inherited Annotation to
        // create the custom annotation therefore not
        // inherited by InheritedAnnotationDemo class. When
        // we use getAnnotation() now, returns null.
        System.out.println(
            new Super().getClass().getAnnotation(
                CustomAnnotation.class));
 
        // Obtaining the class class name and
        // printing the annotation info about the annotation
        // info attached to the Super class
        Class obj = Super.class;
 
        // Calling the Method 2 to
        // print the annotation state
        printAnnotationState(obj);
    }
 
    // Method 2
    // To print the annotation state
    static void printAnnotationState(AnnotatedElement ann)
    {
 
        // Obtaining all the annotations attached to the
        // passed element and storing it in an array
        Annotation[] annotationsArray
            = ann.getAnnotations();
 
        // Iterating on all the annotations stored inside of
        // the array above and printing their information
        for (Annotation annotation : annotationsArray) {
 
            // Print and display name and value of the the
            // annotation
            System.out.println(
                "Name of the annotation : "
                + annotation.annotationType());
            System.out.println(
                "Value : "
                + ((CustomAnnotation)annotation).value());
        }
    }
}


输出
@CustomAnnotation(value="Sky is limitless")
@CustomAnnotation(value="Sky is limitless")
Name of the annotation : interface CustomAnnotation
Value : Sky is limitless

案例2:不使用@inherited Annotation

在下面提到的代码示例中,除了我们没有使用 @Inherited 注释来注释我们的 CustomAnnotation 之外,一切都相同。因此,InheritedAnnotationDemo 类不会继承 CustomAnnotation。因此,当我们对 InheritedAnnotationDemo 类使用 getAnnotation() 方法时,它不会返回 CustomAnnotation。在这种情况下,我们没有使用任何注释来注释 InheritedAnnotationDemo 类,它只是返回 null。

例子 :

Java

// Java Program to Illustrating Use of Custom Annotations
// Without @inherited annotation
 
// Importing required classes from java.lang package
import java.lang.annotation.*;
import java.lang.reflect.AnnotatedElement;
 
// Creating our single valued custom
// annotation without @inherited annotation
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
// Custom annotation
@interface CustomAnnotation
{
    String value() default "GFG";
}
 
// Annotating the super class using our
// custom annotation
@CustomAnnotation(value = "Sky is limitless")
 
// Class 1
class Super {
}
 
// Class 2
// Main class
// This is our derived class which inherits the
// the Super class but does not inherit the
// CustomAnnotation
public class InheritedAnnotationDemo extends Super {
    public static void main(String[] arg) throws Exception
    {
 
        // Printing the annotation used to annotated the
        // Super and InheritedAnnotationDemo classes
        System.out.println(
            new InheritedAnnotationDemo()
                .getClass()
                .getAnnotation(CustomAnnotation.class));
 
        // As we haven't used the @Inherited Annotation to
        // create the custom annotation therefore not
        // inherited by InheritedAnnotationDemo class. When
        // we use getAnnotation() now, returns null.
        System.out.println(
            new Super().getClass().getAnnotation(
                CustomAnnotation.class));
 
        // Obtaining the class class name and
        // printing the annotation info about the annotation
        // info attached to the Super class
        Class obj = Super.class;
 
        // Calling the Method 2 to
        // print the annotation state
        printAnnotationState(obj);
    }
 
    // Method 2
    // To print the annotation state
    static void printAnnotationState(AnnotatedElement ann)
    {
 
        // Obtaining all the annotations attached to the
        // passed element and storing it in an array
        Annotation[] annotationsArray
            = ann.getAnnotations();
 
        // Iterating on all the annotations stored inside of
        // the array above and printing their information
        for (Annotation annotation : annotationsArray) {
 
            // Print and display name and value of the the
            // annotation
            System.out.println(
                "Name of the annotation : "
                + annotation.annotationType());
            System.out.println(
                "Value : "
                + ((CustomAnnotation)annotation).value());
        }
    }
}
输出
null
@CustomAnnotation(value="Sky is limitless")
Name of the annotation : interface CustomAnnotation
Value : Sky is limitless