Java8 new feature annotation-power node

Added FunctionalInterface annotation

Functional interface annotations, prepared for Labmda expressions.

When only one method in the interface is an abstract method, the interface can be declared as a functional interface.

package com.wkcto.annotaitons;

/**

  • Functional interface

  • Author: Power Node Lao Cui
    */
    @FunctionalInterface //Annotation declares that the interface is a functional interface
    public interface MyInterface2 { void m1();

    default void dm(){ System.out.println("default modified method has default method body"); } }


package com.wkcto.annotaitons;

/**

  • Functional interface can use Labmda expressions

  • Author: Power Node Lao Cui
    */
    public class Test01 { public static void main(String[] args) { //Interface assignment anonymous internal class object MyInterface2 mi2 = new MyInterface2() { @Override public void m1() { System.out .println("Overriding the abstract method of the interface in the anonymous inner class"); } }; mi2.m1(); //Execute the method of the anonymous inner class object








     //MyInteface2接口声明为了函数式接口,可以直接给接口引用赋值Labmda表达式
     mi2 = () -> System.out.println("给接口引用赋值Lambda表达式");
     mi2.m1();
    

    }
    }

Enhancements to meta annotations

JDK8 expands the scope of annotations, and enhances two enumeration values ​​in the ElementType enumeration type:

ElementType.PARAMETER means that the annotation can be written in the declaration statement of the type variable.

ElementType.USE, which means that the annotation can be written in any statement using the type.

Repeatable meta-annotation has been added. In versions before JDK8, the same annotation can only be used once at the same location, not multiple times. Repeated annotations are introduced in JDK8, which means that an annotation can be reused multiple times in the same location.

package com.wkcto.annotaitons;

import java.lang.annotation.*;

/**Custom repeatable annotation

  • Author: Power Node Old Cui
    */
    @Repeatable(RoleAnnotions.class)
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE,ElementType.METHOD})
    public @interface MyAnnotation { String role(); //Define role attributes } package com.wkcto.annotaitons;


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

/**

  • Define a container that can contain several MyAnnotation annotations
  • Author : 动力节点老崔
    */
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE,ElementType.METHOD})
    public @interface RoleAnnotions {
    MyAnnotation[] value();
    }
    package com.wkcto.annotaitons;

/**

  • Use repeated annotations to decorate a class
  • Author : 动力节点老崔
    */
    @MyAnnotation( role = “Husband”)
    @MyAnnotation(role = “Son”)
    @MyAnnotation(role = “Father”)
    public class Person {
    }
    package com.wkcto.annotaitons;

/**

  • Read re-annotated information through reflection

  • Author: Power Node Lao Cui
    */
    public class Test02 { public static void main(String[] args) { //Create Class object Class<?> claxx = Person.class;


     //获得指定的自定义注解
     MyAnnotation[] myAnnotations = claxx.getDeclaredAnnotationsByType(MyAnnotation.class);
     //遍历数组,打印角色属性
     for (MyAnnotation  annotation : myAnnotations){
         System.out.println( annotation.role());
     }
    

    }
    }

Guess you like

Origin blog.csdn.net/weixin_49543720/article/details/111386233