Detailed explanation of @interface usage (reproduced)

Another article summarizes the content related to the interface in detail: Annotation Detailed Explanation

First of all, @interface is not an interface but an annotation class. The function added after jdk1.5, when using @interface custom annotation, automatically inherits the java.lang.annotation.Annotation interface, and the compiler automatically completes other details. Compile and you can see that the source code is as follows:

//保留的环境
@Retention(RUNTIME)
//注释起作用的位置,此处表示它只能给类、接口、枚举注解
@Target(TYPE)
public @interface Test {
    
    
  
  public int id() default -1;
 
  public String msg() default "Hi";
}

After compiling, use javap -p Test.class to get the following results:

insert image description here

When defining an annotation, it cannot inherit other annotations or interfaces. @interface is used to declare an annotation, and each method in it actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter (the return value type can only be the basic type, Class, String, enum). The default value of the parameter can be declared by default.

The following content is specifically emphasized in the Java API documentation:

Annotation is a common extension interface for all annotation types. Note that manually extending this interface does not define annotation types. Also note that this interface itself does not define annotation types. More information on annotation types can be found in section 9.6 of the Java™ Language Specification.

@interface can achieve three functions:

(1) Declaration class: Class
(2) Declaration category: Category
(3) Declaration extension: Extension

1. Declaration class:

This is more commonly used:

@interface SomeClass : NSObject {
}

2. Declaration category:

(1) The category can add methods to the class or rewrite the methods of the class without changing the original class code.
(2) Categories can only add or override methods, but cannot add variables.
(3) Some netizens said that setting the category name to "Private" can make the method added in the category a private method, which is not true (verified by the actual code).
(4) If the existing method of the rewriting class is rewritten, the rewritten method will take effect in the entire operating environment, and there is no need to import the implementation class where it is used; if a
new method is added to the class, then It needs to be imported where it is used.
(5) Code:
@interface ClassName (category name) { }

3. Declare the extension:

(1) The grammatical difference between extension and category is very simple, that is, the category name is omitted and only parentheses are reserved.
(2) The extension only adds the method and variable declaration of the original class, but does not include the implementation. Therefore, the extension does not have an independent implementation (@implementation), but shares an implementation with the original class.
(3) The extension can not only add methods on the basis of the original class, but also increase variables.
(4) If the extension is written into the implementation file, the added variables and methods are private variables and private methods.
(5) Code:
@interface ClassName(){ } ————————————————— Copyright Notice: This article is the original article of CSDN blogger "little-motor", following CC 4.0 BY- SA copyright agreement, please attach the original source link and this statement for reprinting. Original link: https://blog.csdn.net/qq_39385118/article/details/80599837



Guess you like

Origin blog.csdn.net/qq_43575801/article/details/128835642