java-based enumeration and usage notes

Enumeration Enum

concept

It is used to define a finite number, in the form of discrete, very clear expression of the amount. (A role similar to static final static constant, but it will integrate)

Enumeration of use

  • Before not enumerate, define static constants required to define static final, but the number of a long time, seem cumbersome
public static final int VIDEO = 1;//视频
public static final int AUDIO = 2;//音频
public static final int TEXT = 3;//文字
public static final int IMAGE = 4;//图片
  • After the enumeration there, we can put all static constants are enumerated type classes
public enum Enums{
	VIDEO,AUDIO,TEXT,IMAGE;
}

// 调用方式  枚举名.常量
Enums.VIDEO;

enum built-in method

  • int compareTo (E e) Compares this enum with the specified object (the default enumeration class that there are arranged in order from left)

    • (Enums.VIDEO).compareTo((Enums.AUDIO))

  • ** Class getDelcaringClass () ** Returns enum constant Class object type corresponding to this enumeration.

    • Class<?> clazz = (Enums.VIDEO).getDeclaringClass();

  • ** String name () / toString () ** Returns the name of this enum constant, declared in its enum declaration.

    • Enums.MONDAY.name() // MONDAY

      Enums.MONDAY.toString() // MONDAY

  • int ordinal () Returns enum constant ordinal

    • Enums.MONDAY.ordinal() // 0

  • ** static T [] values ​​() ** return all the enumeration values.

    • Enums[] values = Enums.values();

  • ** static <T extends Enum> T valueOf (Class Enums, String name) ** Returns enum type enum constant with the specified name.

    • Enums d = Enum.valueOf(Enums.class,Enums.MONDAY.name());

When there is an enumeration class constructor, the default constructor private private type

public enum Enums{
    // 枚举有构造参数时
	VIDEO(1, "视频"), AUDIO(2, "音频"), TEXT(3, "文本"), IMAGE(4, "图像");
    
    int value;
    String name;
    
    Enums(int value,String name){
        this.value = value;
		this.name = name;
    }
    
    // 存在构造器时,如果需要获取构造器中的值,需要让外界有获取方法
    public int getValue() {
		return value;
	}
	
	public String getName() {
		return name;
	}
}

public static void main(String[] args) {
        Enums[] values = Enums.values();
        for (Enums value : values) {
            System.out.print(value+":");
            System.out.println(value.getName());
        }
}


VIDEO	视频
AUDIO	音频
TEXT	文本
IMAGE	图像

Annotations Annotation

concept

Description of the process. To the computer to see

Role Category:

  • ① written document: a document generated by annotating code identification document generation doc [documentation]
  • ② code analysis: analysis of the code by annotating code using reflection [ID]
  • ③ compilation checking: by annotating code identified so that the compiler can compile basic checking [Override]

JDK predefined number of notes

  • @Override: a method for detecting the labeled annotation whether inherited from the parent class (interface)
  • @Deprecated: This comment marked content, expressed obsolete
  • @SuppressWarnings: suppress warnings
  • @SafeVarargs: Java 7 support start, ignore the warning any method or constructor parameter type variable call generated.
  • @FunctionalInterface: Java 8 began to support, identify an anonymous function or a function interface.
  • @Repeatable: Java 8 began to support, identify certain notes can be used multiple times on the same statement.

Custom annotation

  • Essence: annotation is essentially a interface that inherits default Annotation Interface

  • public interface MyAnno extends java.lang.annotation.Annotation {}

  • Properties: abstract interface methods

  • Claim:

  1. Return Value Type attribute have the following values

    • Basic data types
    • String
    • enumerate
    • annotation
    • More than one type of array
  2. The property is defined when you use need to give the property assignment

    1. If the definition of property, use the default keyword to attribute default initialization value, when using annotations, you can not assign attributes.
    2. If only the need to assign an attribute, and a value of the name attribute, the value may be omitted , directly defined value.
    3. Assigning an array using {value} package. If the array has only one value, it can be omitted {}
public @interface MyAnno {
//    int show1();
//
//    boolean show2();

    /*
    属性的返回值类型有下列取值
    * 基本数据类型
    * String
    * 枚举
    * 注解
    * 以上类型的数组
     */
//    int age();
//    String name();
//    String url() default "/*";
//    Sex sex() default Sex.Female;
//    MyAnno2 anno2();
    String[] arr();
    String value();//value属性非常特殊,当只有一个属性的时候,可以省略
    String url() default "/*";
    Sex sex() default Sex.Female;

}

Annotations are used to describe annotation: a meta-annotation

  • @Target: annotation can describe the role of location
    • ElementTypeValue:
    • ElementType.TYPE Annotations can only be modified to specify the class or interface - Key
    • ElementType.FIELD Annotations can only be modified to specify member properties - Key
    • ElementType.METHOD Annotations can only specify modification methods - focus
    • ElementType.PARAMETER Notes parameter specifies the method can only be modified
    • ElementType.CONSTRUCTOR Annotations can only specify the modified constructor
    • ElementType.LOCAL_VARIABLE Annotations can only be modified to specify local variables
    • ElementType.TYPE_USE Specify annotations can be modified after all --JDK1.8 have
  • @Retention: Description stage annotations are reserved
  • RetentionPolicy.SOURCE Annotation information is retained in the source file
  • RetentionPolicy.CLASS Retained in the class file
  • RetentionPolicy.RUNTIME Annotation information remains in the running, with the use of reflection - Key

@Retention (RetentionPolicy.RUNTIME): annotation currently being described, will be retained to the class bytecode files, and read into the JVM

  • @Documented: description notes whether the document is drawn into the api
  • @Inherited: description notes whether inherited by subclasses

Use annotations in the program

Determine whether there is a comment

  • isAnnotationPresent(Class)

Gets the specified comment

  • getAnnotation (notes .Class)

Abstract method call annotation get property values ​​configured

  • . getAnnotation (Class) method is annotated as: modified annotation portion (Type / Method / Field) .getAnnotation ( annotated .class) .age ();
Published 12 original articles · won praise 0 · Views 53

Guess you like

Origin blog.csdn.net/DavinDeng/article/details/104918961