1。概要
注釈はメソッドで定義できます。注釈はクラスに相当します。
一般的に使用される注釈:
@Override:再介入クラスの方法を意味します。
これは、プロンプトが間違っている場合、親クラスのメソッドを上書きすることもできます。上書きされた親クラスの方法。
@suppresswarnings( "deprecation"):コンパイラ警告をキャンセルします(たとえば、使用している方法は時代遅れです)
@deprecated:このステートメントはメソッドの上部にも配置されており、この方法が時代遅れであるか、クラスで使用されていることを示しています。
コードコピーは次のとおりです。
java.util.arraylistをインポートします。
java.util.listをインポートします。
パブリッククラスのannotationdemo {
/*
*コレクションの場合、ストレージタイプが指定されていない場合、セキュリティ警告があります。
*セキュリティの警告を求めたくない場合は、@SuppressWarnings(パラメーター)をクラスまたはメソッドに追加します。
*/
@suppresswarnings( "unchecked")
public static void main(string [] args){
リストリスト= new arrayList();
}
}
2。カスタムアノテーション
1。フォーマット
permissions @interfaceアノテーション名{}
ステップ:
注釈クラスを定義--->アプリケーションアノテーションクラスのクラスを定義します--->アプリケーションアノテーションクラスのクラスを反映するクラス(このクラスは別々に定義するか、アプリケーションアノテーションクラスでテストすることができます)
コードコピーは次のとおりです。
java.lang.annotation.retentionをインポートします。
importjava.lang.annotation.retentionPolicy;
//この注釈は、bytecodeに保持されるように定義します
@retention(RetentionPolicy.Runtime)
public @interface myannotation {
}
@myannotation
//アプリケーション定義の注釈クラス
パブリッククラスApplyMyAnnotation {
public static void main(string [] args){
if(applymyannotation.class.isannotationpresent(myannotation.class)){//このクラスに指定されたアノテーションクラスがあるかどうかを判断します
myannotation annotation =(myannotation)applymyannotation.class
.getannotation(myannotation.class);
System.out.println(annotation);
}
}
}
2。宣言サイクル
形式:例:@retention(retentionPolicy.class)
カスタムアノテーションクラスでサイクルを定義する、@retention(パラメータータイプ)パラメータータイプはretentionPolicyです
retentionPolicy.class:クラスファイルでは、仮想マシンはランタイム中に注釈を保持しません
retentionPolicy.runtime:クラスファイルでは、ランタイム中に仮想コメントが保持されます。
RetentionPolicy.Source:ソースファイルで、注釈を破棄します
抑制とオーバーライドはretentionPolicy.Sourceです。
廃止されたのは、runtimeのruntimeです。
デフォルトはretentionPolicy.classです:
3.ターゲットを指定します
形式:例:method @target(elementtype.method)
定義された注釈によってメンバーに注釈を付けることができます。この注釈が宣言されていない場合、プログラム要素に配置できます。
パッケージ、インターフェイス、パラメーター、メソッド、ローカル変数、フィールドなどです。
コードコピーは次のとおりです。
//この注釈は、bytecodeに保持されるように定義します
@retention(RetentionPolicy.Runtime)
@Target({elementType.Method、elementType.Type})//メソッドとクラスで定義して、タイプを表すことができます
public @interface myannotation {
}
@myannotation
//アプリケーション定義の注釈クラス
パブリッククラスApplyMyAnnotation {
@myannotation //メソッドで定義されています
public static void main(string [] args){
if(applymyannotation.class.isannotationpresent(myannotation.class)){//このクラスに指定されたアノテーションクラスがあるかどうかを判断します
myannotation annotation =(myannotation)applymyannotation.class
.getannotation(myannotation.class);
System.out.println(annotation);
}
}
}
3.アノテーションに属性を追加します
1。タイプ
注釈の属性設定は、8つの基本データ型、文字列、列挙、注釈、クラス、配列タイプ、
2。注意を払ってください
アノテーションに属性が1つしかない場合、または1つの属性のみを割り当てる必要がある場合は、属性名を指定せずに呼び出されたときに直接記述できます。
注釈付き属性が配列タイプであり、割り当て時に1つの値のみが割り当てられている場合、{}は省略できます。
3。例
3.1.Attribute Type(is string)
コードコピーは次のとおりです。
java.lang.annotation.ElementTypeをインポートします。
java.lang.annotation.retentionをインポートします。
java.lang.annotation.retentionPolicyをインポートします。
Java.lang.Annotation。*;
//この注釈は、bytecodeに保持されるように定義します
@retention(RetentionPolicy.Runtime)
public @interface myannotation {
string値();
string color()デフォルト "red"; //デフォルト値を設定します "red"
}
@myannotation( "Java")
パブリッククラスApplyMyAnnotation {
public static void main(string [] args){
/**
*これは、クラスでの注釈を取得することであり、メソッドの注釈を取得することもできます。
*/
if(applymyannotation.class.isannotationpresent(myannotation.class)){//このクラスに指定されたアノテーションクラスがあるかどうかを判断します
myannotation annotation =(myannotation)applymyannotation.class
.getannotation(myannotation.class);
System.out.println( "value ="+annotation.value());
System.out.println( "color ="+annotation.color());
}
}
}
結果:
value = java
色=赤
呼び出しプログラムから、1つの属性のみを割り当てることができる場合、属性名を省略できることもわかります。それ以外の場合は @ annotationクラス(属性name = value)
3.2包括的なタイプ
コードコピーは次のとおりです。
/*enum class*/
パブリックエインムウィーク{
太陽、月。
}
/**
*注釈クラス
*/
public @interface annotationText {
string値();
}
java.lang.annotation.ElementTypeをインポートします。
java.lang.annotation.retentionをインポートします。
java.lang.annotation.retentionPolicyをインポートします。
Java.lang.Annotation。*;
//この注釈は、bytecodeに保持されるように定義します
@retention(RetentionPolicy.Runtime)
public @interface myannotation {
string値();
string color()デフォルト "red"; //デフォルト値を設定します "red"
週の週()デフォルトウィークモン; //列挙タイプ
int [] array()default {1,2,3}; //配列タイプ
annotationText Annotation()default @annotationText( "my");
class classdemo()default integer.class; // class type
}
@myannotation(value = "java"、color = "green"、week = week.sun、array = 5、annotation =@annotationText( "you")、classdemo = string.class)// array = {4,5、 6}
パブリッククラスApplyMyAnnotation {
public static void main(string [] args){
/**
*これは、クラスでの注釈を取得することであり、メソッドの注釈を取得することもできます。
*/
if(applymyannotation.class.isannotationpresent(myannotation.class)){//このクラスに指定されたアノテーションクラスがあるかどうかを判断します
myannotation annotation =(myannotation)applymyannotation.class
.getannotation(myannotation.class);
System.out.println( "value ="+annotation.value());
System.out.println( "color ="+annotation.color());
System.out.println( "week ="+annotation.week());
system.out.println( "array length ="+annotation.array()。length);
system.out.println( "annotation type value ="+annotation.annotation()。value());
system.out.println( "class type value ="+annotation.classdemo());
}
}
}
結果:
コードコピーは次のとおりです。
value = java
color =緑
週=太陽
配列長= 1
注釈タイプ値= you
クラスタイプvalue = classjava.lang.string
4。メソッドの注釈
コードコピーは次のとおりです。
importjava.lang.annotation.retention;
importjava.lang.annotation.retentionPolicy;
/**
*注釈クラス
*/
@retention(RetentionPolicy.Runtime)
public @interface annotationText {
stringvalue();
}
publicClassApplyMyAnnotation {
publicStaticVoidMain(string [] args)throwsexception {
methodmethodshow = applymyannotation.class.getmethod( "show");
annotationTextanno = methowshow.getAnnotation(annotationText.class);
System.out.println(anno.value());
}
@AnnotationText( "Java")
publicvoidshow(){
system.out.println( "hello");
}
}
結果:Java