Java枚举类

1 枚举类的定义

  • 为什么会有枚举类?因为有些类的实例是有限的、固定的
  • 使用enum定义枚举类,各个常量间使用逗号分隔
    方式1枚举类Color与主类Test定义在一个java文件的平级
public class Test{
	public static void main(String[] args) {
		System.out.println(Color.BLUE); // BLUE
		System.out.println(Color.RED);	// RED
	}
}

enum Color {
	RED,BLUE,YELLOW;
}

方式2枚举类Color定义在一个独自的java文件中
Test.java文件

public class Test{
	public static void main(String[] args) {
		System.out.println(Color.BLUE);	// BLUE
		System.out.println(Color.RED);	// RED
	}
}

Color.java文件

enum Color {
	RED,BLUE,YELLOW;
}

方式3把枚举类Color作为Test类的内部类

public class Test{
	public static void main(String[] args) {
		System.out.println(Color.BLUE); // BLUE
		System.out.println(Color.RED);	// RED
	}
	
	// 作为内部类
	enum Color {
		RED,BLUE,YELLOW;
	}
}

2 枚举值的使用

2.1 枚举类的values()方法

  • 枚举类的values()方法可以把所有枚举值作为数组返回,可以用来迭代枚举值
public class Test{
	public static void main(String[] args) {
		Color[] arr = Color.values();
		for (Color color: arr) {
			System.out.println(color); // RED, BLUE, YELLOW
		}
	}
	
	enum Color {
		RED,BLUE,YELLOW;
	}
}

2.2 枚举值用于switch

public class Test{
	public static void main(String[] args) {
		Color item = Color.BLUE;
		switch(colorItem) {
			case RED:
				System.out.println("红色");
				break;
			case GREEN:
				System.out.println("绿色");
				break;
			case BLUE:
				System.out.println("蓝色");
				break;
		}
	}
	
	enum Color {
		RED,BLUE,YELLOW;
	}
}

运行的结果是打印"蓝色"

2.3 枚举值的ordinal()方法

  • ordinal()方法可以返回枚举值的索引
public class Test{
	public static void main(String[] args) {
		Color[] arr = Color.values();
		for (Color color: arr) {
			System.out.println(color.ordinal()); // 0,1,2
		}
	}
	
	enum Color {
		RED,BLUE,YELLOW;
	}
}

3 枚举值的变量、成员方法和构造方法

  • 枚举值可以跟普通类一样,有自己的变量、成员方法和构造方法
  • 枚举类的构造方法只能使用private,因为不能让外部调用
  • 构造方法里需要设置this.colorValue = colorValue和this.number=number,不然成员变量colorValue和number没有被设置,getColorValue和getNumber获取不到这两个变量的值。
public class Test{
	public static void main(String[] args) {
		System.out.println(Color.BLUE.getColorValue());	// this is blue
		System.out.println(Color.BLUE.getNumber());	// 102
	}
	
	enum Color {
		RED("this is red", 100),YELLOW("this is green", 101),BLUE("this is blue", 102);
		// 成员变量
		private String colorValue;
		private int number;
		// 构造函数
		private Color(String colorValue, int number) {
			System.out.println("调用构造函数,value是" + colorValue + ", number是" + number);
			this.colorValue = colorValue;
			this.number = number;
		}
		
		public int getNumber() {
			return number;
		}

		public String getColorValue() {
			return colorValue;
		}
	}
}
  • 枚举类的构造方法那里的打印如下
    调用构造函数,value是this is red, number是100
    调用构造函数,value是this is yellow, number是101
    调用构造函数,value是this is blue, number是102
  • 这里说明RED(“this is red”, 100),YELLOW(“this is green”, 101),BLUE(“this is blue”, 102)的每个枚举值都通过构造方法实现了一遍,例如RED(“this is red”, 100)有两个参数,正好通过构造方法实现了这个枚举值
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值