Java language foundation (102)-deep understanding of java enumeration class (implement enumeration by yourself vs java native enumeration)

Application scenarios of enumeration classes

In some cases, we have a few fixed options to choose from. Before there is an enumeration class, we generally choose to define it as a few constants. After there is an enumeration, we can use an enumeration to solve it. For example, when we design a small game, we will use a limited number of directions, each direction defines different actions, so this direction class is the case of a limited number of instances, we can use enumeration to achieve, or simulate enumeration achieve.

Implement enumeration yourself:

package EnumDemo;

public abstract class Director {
	
 // 提供有限的几个选项	
 // 抽象类本不可以new 但此处用匿名内部类的形式实现了Director中的move方法
 // 表面上看似是new了一个Director,实际是new的它的子类对象,用Director接收
 // 用的是多态的思想	
 public static final Director UP = new Director("UP"){
	@Override
	public void move() {
		 System.out.println("向上走");
	} 
 };
 public static final Director DOWN = new Director("DOWN"){
	@Override
	public void move() {
		System.out.println("向下走");
	}
 };
 public static final Director LEFT = new Director("LEFT"){
	@Override
	public void move() {
		System.out.println("向左走");
	}
 };
 public static final Director RIGHT = new Director("RIGHT"){
	@Override
	public void move() {
		System.out.println("向右走");
	} 
 };
 
 private String name;
 private Director(String name){
	 this.name = name;
 }
 public String getName(){
	 return this.name;
 }
 
 // 不同的方向在调用move时处理不同    所以这里定义为抽象方法
 public abstract void move();
 
}
Test demo:

package EnumDemo;

public class DirectorDemo {
 
	public static void main(String[] args) {
		 Director d = Director.UP;
		 System.out.println(d.getName());// UP
		 d.move(); // 向上走
	}

}

Java native enumeration:

package EnumDemo;

public enum DirectorEnum {
 // 枚举项
 UP("UP"){

	@Override
	public void move() {
	 System.out.println("向上走");
	}
	 
 },
 // 带参构造 并且匿名内部类的方式实现抽象方法
 DOWN("DOWN") {
	@Override
	public void move() {
		System.out.println("向下走");
	}
},
LEFT("LEFT") {
	@Override
	public void move() {
	    System.out.println("向左走");
	}
},
RIGHT("RIGHT") {
	@Override
	public void move() {
		System.out.println("向右走");
	}
};
 private String name;
 private DirectorEnum(String name){
	 this.name = name;
 }
 public String getName(){
	 return this.name;
 }
 public abstract void move();
}

Test demo:

package EnumDemo;

public class DirectorEnumDemo {

	public static void main(String[] args) {
		 DirectorEnum dd = DirectorEnum.DOWN;
		 System.out.println(dd.getName());// DOWN
		 dd.move();// 向下走
	}

}

Note: It is not necessary to have abstract methods in the enumeration. The abstract methods in this example are just to illustrate the advanced usage of the enumeration class.


Notes on enumeration:

Use the keyword enum to define an enumeration class

All enumeration classes are subclasses of Enum

The first line of the enumeration class must be an enumeration item

Enumeration classes can have constructors, but they must be private, and the default is also private

Enumeration classes can also have an abstract method, but the enumeration item must override this method


Guess you like

Origin blog.csdn.net/wang740209668/article/details/78369832