JAVA19基础-枚举类型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a592381841/article/details/84780984

枚举类型

一.对比两种方法
利用 public static final 定义常量

public class Light {
	public static final int RED=1;
	public static final int GREEN=2;
	public static final int YELLOW=3;
}

利用枚举类型定义常量

public enum LightA {
	RED,GREEN,YELLOW
}


public enum Light {
    // 利用构造函数传参
    RED (1), GREEN (3), YELLOW (2);
  
    // 定义私有变量
    private int nCode ;
  
    // 构造函数,枚举类型只能为私有
    private Light( int _nCode) {
      this . nCode = _nCode;
    }
  
    @Override
    public String toString() {
      return String.valueOf ( this . nCode );
    }
  }

猜你喜欢

转载自blog.csdn.net/a592381841/article/details/84780984