"JavaEE" eighth week day5 study notes - enumerations, annotations, inner classes

First, enumeration

(A) enumeration concept


Enumeration: enum (introduced by JDK1.5 version), is a special data structure, having the characteristics of a Java class, but there is more constrained than Java classes.
Creates an enum:

public enum TypeEnum {
    VIDEO,AUDIO,TEXT,IMAGE;
}

Enumeration can have variables and methods, and can implement the interface, but java.lang.Enum enumerate inherited from class, and therefore can not inherit from other classes.
If you add the parameter enumeration value, you must enumerate the constructor increase, equivalent to the self-constructor calls pass parameters.

public enum TypeEnum {
	VIDEO(1, "视频"), AUDIO(2, "音频"), TEXT(3, "文本"), IMAGE(4, "图像");
	int value;
	String name;
	TypeEnum(int value, String name) {
		this.value = value;
		this.name = name;
	}
	public int getValue() {return value;}
	public String getName() {return name;}
}

Conventional method (B) enumerated


  • int compareTo (E o): Built comparison method
  • Class getDeclaringClass (): Returns the Class object of the enum type
  • String name (): Returns the enum constant name
  • int ordinal (): enum constant number, 0 is the first
  • String toString (): Returns the enum constant name (JDK recommended method!)
  • static <T extends Enum> T valueOf (Class enumType, String - name): Returns the enumeration constants enumType "name" name
  • static T [] values ​​(): returns an enumeration of all the values

Second, notes

(A) notes concept


Notes: Annotation (introduced by JDK1.5 version), metadata, code-level explanations, and interfaces, classes at the same level, notes the first character @, the label is similar to life, to parse (check) code.

(B) common comment


  • @Override: Inheritance
  • @Deprecated: Obsolete
  • @SuppressWarnings: inhibition (parameter passing is usually "all")
  • @SafeVarargs: Ignore generic alarm
  • @FunctionalInterface: identity anonymous function
  • @Repeatable: annotation may be repeated (e.g., an array of two annotated notes, two notes by "an annotation Repeatable reflection", the incoming end of the variable storage)

(C) custom annotation


public @interface 注解名称{
属性列表;
}

Comment on the nature of the interface, inherited from the Annotation interface.
Return Value property requirements: the basic data types, String, enumeration, annotation, an array of several of the foregoing
property attributes assigned:
1. If the default keyword assigns them may not be assigned when using annotations;
2. if only the need to assign an attribute name and attribute value, then the assignment can be omitted value;
used {3. array assignment "value 1", "value 2"}, if only one value is input, then {} may be omitted.

(D) meta-annotation


Yuan notes meanings: for the annotation describing annotations.
1. @ Target: notes describe the role of point

  • ElementType.TYPE: Notes can only modify classes and interfaces
  • ElementType.FIELD: annotation properties can only be modified
  • ElementType.METHOD: Notes only modification methods
  • ElementType.PARAMETER: Notes only modifies parameters
  • ElementType.CONSTRUCTOR: Notes only modifies the constructor
  • ElementType.LOCAL_VARIABLE: Notes only modifies the local variable
  • ElementType.TYPE_USE: Notes can modify all the content (JDK1.8)

Ongoing Phase Description annotation: 2. @ Retention

  • RetentionPolicy.SOURCE: Notes to retain the source file
  • RetentionPolicy.CLASS: Notes reserved to compile the file ".class"
  • RetentionPolicy.RUNTIME: Notes held until runtime (RAM)

3. @ Documented: whether the notes are extracted to describe the API documentation
4. @ Inherited: whether the description of annotations inherited by subclasses

(E) parsing comment


1. Get the object (Class, Method, Field) annotation defines the location
2. determines whether there annotation: isAnnotationPresent (Object);
3. acquiring annotation: getAnnotation (Object)
method 4. Call annotation object: Property Value Name annotation;

Third, the internal class

Will be placed inside a class definition in another class, called inner classes, inner classes use the most appealing reason: Each inner class can independently inherit an implementation (interface), so regardless of whether the enclosing class has inherited an implementation (interface), for inner classes are not affected. However, inner classes do not recommend nesting, would undermine the structure of the code.

public class Outer{
//外部类区域
	class Inner{
//内部类区域
	}
}

(A) members of the inner class


1. Can be any access modifier.
2. How the outer class access internal class information, after passing through must new new (.) Operator access.
3. The internal class information can be used directly in any external class, property or method if conflict, calling external .This class. Property or method.
4. access by other classes within the class manner:
Outer Outer Outer new new = ();
Outer.Inner Inner Inner outer.new = ();
inner.inner_show (); // common method of accessing the internal members of the class

(B) static inner classes


1. As a static member attribute exists, can be modified at any privileges modifier.
2. The method static inner classes can only access information associated with the external class static.
3. The external reference type = new inner classes within a class type external ();.. Then using the reference member information (properties, methods) call.
OI = new new Outer.Inner Outer.Inner ();
oi.innerShow (); // common method of accessing an internal static class
. 4. Access the static information within the class, the class inner class direct external static information.
Outer.Inner.innerStaticShow (); //At this time, the outer class is not loaded, the class loader internal
5. Static inner class can exist independently without depending on other peripheral class.

(C) local (method) inner classes (rarely encountered)


1. You can not use any access modifier.
2. generates two .class files, one is Outer.class, the other is the Outer $ LocalInner.class.
3. The final type of variable local inner class can access only method declarations.

(D) an anonymous inner class


  1. Anonymous inner classes do not have access modifiers.
  2. When using an anonymous inner class, this new class after the first presence, followed by one or some of the new methods of the class to be rewritten.
Published 26 original articles · won praise 0 · Views 243

Guess you like

Origin blog.csdn.net/u010761121/article/details/103933165