day_09_java Advanced Programming_ Common Classes_ Enumeration Class_ Notes (493~510)

Posted by Warptweet on Thu, 06 Jan 2022 00:16:14 +0100

Enumeration classes:

Use of enumerated classes: When the object of a class is finite, deterministic
Week: Monday,..., Sunday
Gender: Man (male), Woman (female)
Season: Spring (Spring Festival)...Winter (Winter)
Payment methods: Cash (cash), WeChatPay (WeChat), Alipay (Alipay), BankCard (bank card), CreditCard (credit card)
Initiation status: Busy, Free, Vocation, Dimission

Enumeration classes are strongly recommended when you need to define a set of constants

/**
 * Use enum keyword to define enumeration class
 * Description: Defined enumeration classes inherit from Java by default. Lang.Enum class
 *
 * 3. Common methods of Enum classes
 *      values()Method: Returns an array of objects of enumeration type. This method makes it easy to iterate through all the enumerated values.
 *      valueOf(String str): You can convert a string to the corresponding enumeration class object. Requires that the string must be the Name of an enumerated class object. If not, there will be a runtime exception: IllegalArgumentException.
 *      toString(): Returns the name of the current enumeration class object constant
 */
public class SeasonTest1 {
    public static void main(String[] args) {
        Season1 summer = Season1.SUMMER;
        //toString():
        System.out.println(summer.toString());

//        System.out.println(Season1.class.getSuperclass());
        System.out.println("**************************");
        //values(): Returns an array of all enumerated class objects
        Season1[] values = Season1.values();
        for(int i = 0;i < values.length;i++){
            System.out.println(values[i]);
        }
        System.out.println("****************************");
        Thread.State[] values1 = Thread.State.values();
        for(int i = 0;i < values1.length;i++){
            System.out.println(values1[i]);
        }

        //valueOf(String objName): Returns an object whose object name is objName in an enumeration class.
        Season1 winter = Season1.valueOf("WINTER");
        //If there is no objName enumeration class object, throw an exception: IllegalArgumentException
//        Season1 winter = Season1.valueOf("WINTER1");
        System.out.println(winter);

    }
}

//Enumerate classes using enum keyword
enum Season1{
    //1. Provide objects of the current enumeration class, with "," separating, ending objects ";" End
    SPRING("spring","Resuscitation"),
    SUMMER("summer","Sunburn"),
    AUTUMN("Autumn","Bright autumn"),
    WINTER("winter","Snow gleams white");

    //2. Declare the properties of the Season object: private final modifiers
    private final String seasonName;
    private final String seasonDesc;

    //3. Private class constructors and assign values to object properties
    private Season1(String seasonName,String seasonDesc){
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }

    //4. Other Requirements: Get properties of enumerated class objects
    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }

    //4. Other claim 1: Provide toString()
//    @Override
//    public String toString() {
//        return "Season{" +
//                "seasonName='" + seasonName + '\'' +
//                ", seasonDesc='" + seasonDesc + '\'' +
//                '}';
//    }
}

When an enumeration class implements an interface, it allows each object to be overridden differently

enum Season implements  info{
    Spring("c day","1"){
        @Override
        public void show() {
            
        }
    },
    Summer("x day","2"){
        @Override
        public void show() {
            
        }
    },
    Autumn("q day","3"){
        @Override
        public void show() {
            
        }
    },
    Winter("d day","4"){
        @Override
        public void show() {
            
        }
    };

annotation

Note This is just about defining what you want to see Notes from others

Since JDK 5.0, Java has added support for metadata, also known as Annotation.
Annotation is actually a special tag in your code that can be read at compile, class load, run time, and processed accordingly. Using Annotation, programmers can embed additional information in their source files without changing their logic. Code analysis tools, development tools, and deployment tools can be validated or deployed with this supplementary information.
Annotation can be used like modifiers to modify declarations of packages, classes, constructors, methods, member variables, parameters, and local variables, which are stored in Annotation's "name=value" pairs.
In JavaSE, annotations are used for simple purposes, such as marking out obsolete functionality, ignoring warnings, and so on. Annotations play a more important role in JavaEE/Android, such as configuring any aspect of the application, replacing the redundant code and XML configuration left behind in older versions of JavaEE.
Future development models are annotation based, JPA annotation based, Spring2.5 All above are based on notes, Hibernate3.x is also based on annotations in the future, and now Struts2 is partly based on annotations, which is a trend, to some extent: Frame = annotation + reflection + design mode.

Meta Note: A comment that modifies other notes.

Topics: Java Back-end