enumeration
-
Introduction to Enumeration:
Enumeration is also a class and has all its members. Using enumeration types ensures the "limitations", "security" and "correctness" of data information.- Limitation: When an enumeration is defined, it can define a specified range. When an enumeration type is used, or when an enumeration type is used to assign a value, the attributes in the assigned object cannot send changes unless they are sent changes by member attributes within the object. Nevertheless, the defined enumeration type has no effect.
- Security: Enumeration type members are defined in entity types, so they can only use the range of values defined in the enumeration type, which ensures that data interaction rejects the incoming illegal data and cannot be changed arbitrarily. (e.g., defining gender types)
- Correctness: Ensure that the data is correct when it is compiled. (See requirements to define the enumeration content to effectively ensure it).
-
Use scenarios:
--- Values that are deterministic in reality, such as defining gender, defining anomalies, defining provincial addresses of nationality, etc. When defining enumeration, try not to choose a definition that is as broad, variable, and random as possible.
/** *Define a gender enumeration class */ public enum Sex { BOY(18),GIRL(16); public int age; Sex(int age){ this.age = age; } public void showAge(){ System.out.println("age is:" +age); } }
/* *Value Access Specifically See Entity Stu Definition */ public class TestEnum { public static void main(String[] args) { Stu stu = new Stu("stu",Sex.GIRL,15); Stu stu1 = new Stu("stu1",Sex.GIRL,17); //Age members in stu2 use the range of values in the enumeration. Since the types defined by age members in stu2 entity classes are identical, the range of values for the enumeration type can also be used. Stu stu2 = new Stu("stu2",Sex.BOY,Sex.GIRL.age); Stu stu3 = new Stu("stu2",Sex.BOY); System.out.println(stu); //age is 15 System.out.println(stu1); //age is 17 System.out.println(stu2); //age is 16 System.out.println(stu3); //age is 0 } } class Stu{ private String name; private Sex sex; //Define gender using enumeration type private int age; //Compile-time errors require consistency with member types of entity classes, pass or error. public Stu() { } public int getAge() { return age; } public void setAge(int age) { // this.age = sex.age;This block does not affect the value this.age = age; } public Stu(String name, Sex sex, int age) { this.name = name; this.sex = sex; this.age = age; } public Stu(String name, Sex sex) { this.name = name; this.sex = sex; // this.age = sex.age;This block directly affects the value } public String getName() { return name; } public void setName(String name) { this.name = name; } public Sex getSex() { return sex; } public void setSex(Sex sex) { this.sex = sex; } @Override public String toString() { return "Stu{" + "name='" + name + '\'' + ", sex=" + sex + ", age=" + age + '}'; } }
annotation
-
Note Introduction:
- Annotations are a new feature of JDK1.5
- Annotations are equivalent to a tag and are part of a class that can carry some extra information to it.
- Annotations are added to packages, classes, fields, methods, method parameters, and local variables.
- Annotations are for the compiler or jvm to see, which can be used by the compiler or jvm to complete the corresponding functions.
-
meta annotation
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Customize Student Notes * */ @Target({ElementType.METHOD,ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface StuTestAnnotation{ String name() default " "; int age() default 0; char sex() default ' '; }
@StuTestAnnotation(name = "stuClass",age = 15,sex = 'male') public class StudentOfAnnotation { @StuTestAnnotation(name = "stuMethod",age = 16,sex = 'female') public void stuTestAnnotation(){ } }
import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.Arrays; public class StudentMain { /** * isAnnotationPresent:Determine whether the object has a specified comment * getAnnotation:Getting corresponding annotation objects based on annotation types * getDeclaredAnnotations:Gets all the annotations used on the current object and returns an array of annotations, including those inherited by the parent * @param args */ public static void main(String[] args) { //Get Object Class student = StudentOfAnnotation.class; //Determine whether the StuTestAnnotation annotation is used on a class System.out.println("Get on Class:"); if (student.isAnnotationPresent(StuTestAnnotation.class)){ //Obtain the corresponding annotation object from the annotation class object StuTestAnnotation annotation =(StuTestAnnotation) student.getAnnotation(StuTestAnnotation.class); System.out.println(annotation.name()); System.out.println(annotation.age()); System.out.println(annotation.sex()); } //Gets all the annotations used on the current object, and returns the annotation array Annotation[] declaredAnnotations = student.getDeclaredAnnotations(); System.out.println(Arrays.toString(declaredAnnotations)); System.out.println("On acquisition method:"); try { Method method = student.getMethod("stuTestAnnotation"); StuTestAnnotation annotation = method.getAnnotation(StuTestAnnotation.class); System.out.println(annotation.name()); System.out.println(annotation.age()); System.out.println(annotation.sex()); Annotation[] declaredAnnotations1 = method.getDeclaredAnnotations(); System.out.println(Arrays.toString(declaredAnnotations1)); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }