FunctionalInterfaceアノテーションを追加
Labmda式用に準備された機能インターフェイスアノテーション。
インターフェイスの1つのメソッドのみが抽象メソッドである場合、インターフェイスは機能インターフェイスとして宣言できます。
パッケージcom.wkcto.annotaitons;
/ **
-
機能的なインターフェース
-
作成者:Power Node Lao Cui
* /
@FunctionalInterface //アノテーションは、インターフェイスが機能インターフェイスであることを宣言します
public interface MyInterface2 { void m1();default void dm(){ System.out.println( "デフォルトの変更されたメソッドにはデフォルトのメソッド本体があります"); } }
パッケージcom.wkcto.annotaitons;
/ **
-
関数型インターフェースはLabmda式を使用できます
-
著者:電源ノードラオス崔
* /
publicクラスTEST01 { 公共の静的な無効メイン(文字列[] args){ //インターフェイスの割り当て匿名の内部クラスオブジェクトMyInterface2 MI2 =新しいMyInterface2(){ @Overrideます。public void M1(){ のSystem.out .println( "匿名内部クラスのインターフェイスの抽象メソッドをオーバーライドする"); } }; mi2.m1(); //匿名内部クラスオブジェクトのメソッドを実行する
//MyInteface2接口声明为了函数式接口,可以直接给接口引用赋值Labmda表达式 mi2 = () -> System.out.println("给接口引用赋值Lambda表达式"); mi2.m1();
}
}
メタアノテーションの機能強化
JDK8は、アノテーションの範囲を拡張し、ElementType列挙型の2つの列挙値を拡張しました:
ElementType.PARAMETERは、型変数の宣言ステートメントに注釈を記述できることを意味します。
ElementType.USE。これは、型を使用して任意のステートメントに注釈を記述できることを意味します。
繰り返し可能なメタアノテーションが追加されました。JDK8より前のバージョンでは、同じアノテーションは同じ場所で1回だけ使用でき、複数回は使用できません。繰り返しのアノテーションはJDK8で導入されているため、同じ場所で複数回再利用できます。ロケーション。
パッケージcom.wkcto.annotaitons;
importjava.lang.annotation。*;
/ **カスタムの繰り返し可能な注釈
- 作成者:Power Node Old Cui
* /
@Repeatable(RoleAnnotions.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE、ElementType.METHOD})
public @interface MyAnnotation { String role(); //ロール属性を定義}パッケージcom.wkcto.annotaitons;
インポートjava.lang.annotation.ElementType;
インポートjava.lang.annotation.Retention;
インポートjava.lang.annotation.RetentionPolicy;
インポートjava.lang.annotation.Target;
/ **
- 複数のMyAnnotationアノテーションを含むことができるコンテナーを定義します
- 作成者:π力节点老崔
* /
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE、ElementType.METHOD})
public @interface RoleAnnotions { MyAnnotation [] value(); }パッケージcom.wkcto.annotaitons;
/ **
- 繰り返し注釈を使用してクラスを装飾する
- 著者:π力节点老崔
* /
@MyAnnotation(role =“ Husband”)
@ MyAnnotation(role =“ Son”)
@ MyAnnotation(role =“ Father”)
public class Person { }パッケージcom.wkcto.annotaitons;
/ **
-
リフレクションを通じて再注釈付けされた情報を読む
-
作成者:Power Node Lao Cui
* /
public class Test02 { public static void main(String [] args){ //クラスオブジェクトクラスを作成<?> claxx = Person.class;
//获得指定的自定义注解 MyAnnotation[] myAnnotations = claxx.getDeclaredAnnotationsByType(MyAnnotation.class); //遍历数组,打印角色属性 for (MyAnnotation annotation : myAnnotations){ System.out.println( annotation.role()); }
}
}