Enumerations can be defined this way in C

enum color {
    RED=0, GREEN, BLUE, YELLOW
} col;
Copy the code

The keyword enum defines enumeration. When defining enumeration, declare the enumeration variable COL.

Note: Enumerators in C are automatically incremented by one based on context (GREEN = 1, BLUE = 2, etc.).

C switch statements support enumerated types

#include<stdio.h>
int main() {
enum color {
    RED=0, GREEN, BLUE, YELLOW
} col;

int cl;

printf("0=red,1=green,2=blue,3=yellow. seclect:\n");
scanf("%d",&cl);

col=(enum color) cl;

switch(col) {
    case RED:
        printf("the color is red\n");
        break;
    case GREEN:
        printf("the color is green\n");
        break;
    case BLUE:
         printf("the color is blue\n");
        break;
    case YELLOW:
        printf("the color is yellow\n");
        break;
    defalut:
        printf("no this color\n");
        break;
}
   return 0;
}
Copy the code

So enumerations in Java are similar, but not identical.

Enumerations are also defined in the Java language using the keyword enum

public enum Color {
	RED, GREEN, BLUE, YELLOW;
}
Copy the code

The above defines an enumerated type Color (class, so to speak, color.class after compilation).

So this definition right here, we could rewrite it this way

public enum Color {
	RED(), GREEN(), BLUE(), YELLOW();
}
Copy the code

At this point you will feel confused (if you are a beginner), why is this ok?

In fact, the members of an enumeration are just enumerated objects, but they are static constants.

To decompile a class file, use the javap command (javap filename < no suffix. Class >) as follows:

We can use ordinary classes to simulate enumerations. Let’s define a Color class.

 public class Color {
	private static final Color RED = new Color();
	private static final Color GREEN = new Color();
	private static final Color BLUE = new Color();
	private static final Color YELLOW = new Color();
}
Copy the code

Compare that and you get the idea.

If you follow this logic, can you add another constructor to it? The answer is yes!

public enum Color {
	RED("red color", 0), GREEN("green color", 1), 
	BLUE("blue color", 2), YELLOW("yellow color", 3);

	Color(String name, int id) {
		_name = name;
		_id = id;
	}

	String _name;
	int _id;
Copy the code

} declares two member variables for Color and constructs a constructor with arguments for it.

If you create an enumeration like this

public enum Color {
	RED("red color", 0), GREEN("green color", 1), 
	BLUE("blue color", 2), YELLOW("yellow color", 3);
}
Copy the code

The compiler will report an error

You can see that enumerations are basically (but not exactly) The same as normal classes.

It is best for a class to privatize its member variables and then provide get and set methods for them.

According to this principle, enum Color can be further written.

public enum Color { RED("red color", 0), GREEN("green color", 1), BLUE("blue color", 2), YELLOW("yellow color", 3); Color(String name, int id) { _name = name; _id = id; } private String _name; private int _id; public void setName(String name) { _name = name; } public void setId(int id) { _id = id; } public String getName() { return _name; } public int getId() { return _id; }}Copy the code

However, the purpose of The Java design enum is to provide a set of constants that users can easily design.

If we were to provide a set method (where the outside world can change its member attributes), it would be a bit of a defeat.

So, we should discard the set method and keep the GET method.

public enum Color {
	RED("red color", 0), GREEN("green color", 1),
	BLUE("blue color", 2), YELLOW("yellow color", 3);

	Color(String name, int id) {
		_name = name;
		_id = id;
	}

	private String _name;
	private int _id;
	
	public String getName() {
		return _name;
	}

	public int getId() {
		return _id;
	}
Copy the code

}

Ordinary classes, we can instantiate, but can we instantiate enumerations?

Before we answer that question, let’s look at the compiled color.class file

public static enum Color {
	RED("red color", 0), GREEN("green color", 1),
	BLUE("blue color", 2), YELLOW("yellow color", 3);

	private String _name;
	private int _id;

	private Color(String name, int id) {
		this._name = name;
		this._id = id;
	}

	public String getName() {
		return this._name;
	}

	public int getId() {
		return this._id;
	}
Copy the code

}

As you can see, the compiler has naughtily added private to its constructor, so we can’t instantiate the enumeration.

All enumerated classes inherit the methods of Enum classes, including toString, equals, Hashcode, and so on.

Because the equals, hashcode methods are final, they cannot be overridden by enumerations (only inherited).

However, you can override the toString method.

For details about Enum source code, see appendix!

So, using different classes of Java to simulate enumerations, it looks something like this

package mark.demo;

import java.util.ArrayList;
import java.util.List;

public class Color {
	private static final Color RED = new Color("red color", 0);
	private static final Color GREEN = new Color("green color", 1);
	private static final Color BLUE = new Color("blue color", 2);
	private static final Color YELLOW = new Color("yellow color", 3);

private final String _name;
private final int _id;

private Color(String name, int id) {
	_name = name;
	_id = id;
}

public String getName() {
	return _name;
}

public int getId() {
	return _id;
}

public static List<Color> values() {
	List<Color> list = new ArrayList<Color>();
	list.add(RED);
	list.add(GREEN);
	list.add(BLUE);
	list.add(YELLOW);
	return list;
}

@Override
public String toString() {
	return "the color _name=" + _name + ", _id=" + _id;
}
Copy the code

The original link: blog.csdn.net/veryitman/a…