JAVA study diary: Notes (4)

Today I am studying the fourth lesson (basic) of annotation

In this note, I have reprinted too many quotes from the original words in the class. The example is done by myself, but I am afraid of infringement.
The main content learned today:

	元注解

Meta annotation:

	通俗来讲就是给注解再添加注解的注解,是JDK自带的注解
	它存在于java.lang.annotation包中。
	主要有以下几种元注解:
				* @Target(靶子,运行目标):注解用在哪里,谁能用谁不能用,对谁有用,对谁没用。
				* @Retention(保留,滞留):注解中数据保存的范围:类中,运行时,源代码。
				* @Inherited(继承):能不能将该注解跟随父类继承到子类中去。
				   @Documented(备有证明文件的):用于生称文档。
				* @Repeatable(重复的):说明当前注解可否在同一个目标上重复使用。
				  @Native(原生的)

Target annotation:

	作用:告诉注解应用的位置
	只有唯一元素value,类型是ElementType枚举的数组
	有效值:
			ANNOTATION_TYPE, CONSTRUCTER(重,构造方法),FIELD(属性,域),LOCAL_VARIABLE(局部变量)
			METHOD,PACKAGE(重),PARAMETER(方法参数),TYPE(重,类,接口都是type里的一种类型)
			TYPE_PARAMETER(JDK8.0+,与泛型有关),TYPE_USE(JDK8.0+,重)

Retention note:

	作用:用于指定注解实例如何被java保留,因为注解中的信息我们迟早是要取出来用的,那么我们注解存放在什么位置就很关键。
	只有一个value元素,类型是RetentionPolicy枚举。
	有效值:
			SOURCE:存放在这里的数据信息将不会被编译到class内,更不可能运行时通过反射来获取其值。
			CLASS:默认保留位置,但是不能在运行时通过反射获取该信息。
			RUNTIME:我们自定义的注解,事实上多数都是希望在运行时能够获取得到的。	

Inherited annotation:

	表示注解是否可以被子元素继承	。

Documented notes:

	表示javacod是否为实例产生文档。

Repeatable annotation:

	作用:java8新增注解,表示注解是否可以重复使用再同一个元素之上。
	使用:
			声明注解,使用@Repeatable修饰,指定其值为包含元素的类型是该注解数组的注解类。
			声明注解,定义其value属性,类型为前面注解的数组。

Native annotation:

	表示使用该注解的变量可以被本地代码访问。

GameListener annotation (annotated by the above meta annotation):

package LessonForAnnotation04;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//target中只有一个元素value所以可以直接赋值

@Target({
    
    ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.METHOD,ElementType.TYPE,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Repeatable(GameListener02.class)
public @interface GameListener 
{
    
    

}

@Target({
    
    ElementType.CONSTRUCTOR,ElementType.FIELD,ElementType.METHOD,ElementType.TYPE,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface GameListener02
{
    
    
	GameListener[] value();
}

Game01 (main test category):

package LessonForAnnotation04;

@GameListener
public class Game01 
{
    
    
	@GameListener
	private String gameName;
	private String pricel;
	
	public Game01()
	{
    
    
		
	}
	
	@GameListener
	public Game01(String gameName, String pricel) 
	{
    
    
		super();
		this.gameName = gameName;
		this.pricel = pricel;
	}

	@GameListener
	@GameListener
	public String getGameName() 
	{
    
    
		return gameName;
	}

	
	public void setGameName(@GameListener String gameName) {
    
    
		this.gameName = gameName;
	}

	public String getPricel()
	{
    
    
		return pricel;
	}

	public void setPricel(String pricel)
	{
    
    
		this.pricel = pricel;
	}
}

//添加了@Inherited注解之后@GameListener自动被继承过来不用在这个子类前再声明一遍@GameListener
class UserInfor extends Game01
{
    
    
	
}

部分文字来源于:
	咕嘟咖啡杨海滨老师 — 《java编程语言高级特性》
	在这里十分感谢老师能够给我带来学习的激情。

2020.10.13
can reprint my study diary but please indicate the source, thank you.
complete

Guess you like

Origin blog.csdn.net/SIESTA030/article/details/109059265