枚举

本文详细介绍了枚举类型的概念、使用方法及其特性。包括枚举类型的定义、枚举项的使用、成员变量和方法的添加、抽象方法的实现、在switch语句中的应用以及一些重要的内置方法如name(), ordinal(), valueOf()和values()的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

枚举的概述:将变量的值一一列举出来,变量的值只限于列举出来的值的范围内.

枚举是一种特殊的类,用关键字enum.

public enum Direction {
	LEFT, TOP, RIGHT, BOTTOM;
}
enum中列举出来的项,如LEFT,TOP等叫做枚举项.

所有的枚举类都是enum的子类.

枚举中还可以有自己的成员变量,方法以及构造方法.

注意枚举类不能被继承,所以枚举类的构造都是私有的,用private修饰.

如果枚举类的构造方法有带参,那么枚举项后面也需要跟参数.

public enum Direction {
	LEFT("left"), TOP("top"), RIGHT("right"), BOTTOM("bottom");

	private String name;

	private Direction(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
}
public class EnumTest {
	public static void main(String[] args) {
		Direction left = Direction.LEFT;
		System.out.println(left.getName());
		System.out.println(Direction.BOTTOM.getName());
	}
}
运行结果如下:

left
bottom

枚举类还可以加抽象方法

加抽象方法时,枚举项需要复写相应的抽象方法.

public enum Direction {
	LEFT("left") {
		@Override
		public void show() {
			System.out.println("左");
		}
	},
	TOP("top") {
		@Override
		public void show() {
			System.out.println("上");
		}
	},
	RIGHT("right") {
		@Override
		public void show() {
			System.out.println("右");
		}
	},
	BOTTOM("bottom") {
		@Override
		public void show() {
			System.out.println("下");
		}
	};

	private String name;

	private Direction(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public abstract void show();
}

public class EnumTest {
	public static void main(String[] args) {
		Direction left = Direction.LEFT;
		System.out.println(left.getName());
		left.show();
	}
}
运行结果如下:

left

枚举在switch中的运用

public void judge(Direction direction) {
		switch (direction.getName()) {
		case "left":
			System.out.println("left");
			break;
		case "top":
			System.out.println("top");
			break;
		case "right":
			System.out.println("right");
			break;
		case "bottom":
			System.out.println("bottom");
			break;
		default:
			break;
		}
	}
或者

public void judge(Direction direction) {
		switch (direction) {
		case LEFT:
			System.out.println("left");
			break;
		case TOP:
			System.out.println("top");
			break;
		case RIGHT:
			System.out.println("right");
			break;
		case BOTTOM:
			System.out.println("bottom");
			break;
		default:
			break;
		}
	}
枚举类的注意事项:

1.枚举类的第一行上必须是枚举项,最后一个枚举项后面的分号可以省略,但是如果枚举类有其他

的东西时,最后一个分号就不能省略.所以建议都不要省略.

2.枚举类在声明时枚举项是有次序的,序号从0开始.

3.枚举类的compareTo方法比较的就是枚举项的次序.

public class EnumTest {
	public static void main(String[] args) {
		Direction left = Direction.LEFT;
		Direction right = Direction.RIGHT;
		System.out.println(left.compareTo(left));
		System.out.println(left.compareTo(right));
		System.out.println(right.compareTo(left));	
	}
}
运行结果如下:

0
-2
2

4.枚举类中name()方法

public class EnumTest {
	public static void main(String[] args) {
	
		Direction left = Direction.LEFT;
		System.out.println(left.name());
		Direction top = Direction.TOP;
		System.out.println(top.name());
		Direction right = Direction.RIGHT;
		System.out.println(right.name());
		Direction bottom = Direction.BOTTOM;
		System.out.println(bottom.name());
	}
}
运行结果如下:

LEFT
TOP
RIGHT
BOTTOM
5.枚举类 的ordinal()方法

public class EnumTest {
	public static void main(String[] args) {
	
		Direction left = Direction.LEFT;
		System.out.println(left.ordinal());
		Direction top = Direction.TOP;
		System.out.println(top.ordinal());
		Direction right = Direction.RIGHT;
		System.out.println(right.ordinal());
		Direction bottom = Direction.BOTTOM;
		System.out.println(bottom.ordinal());
	}
}
运行结果如下:

0
1
2
3

即ordinal()方法打印的就是枚举常量的序数.

6.枚举类的valueOf方法,返回的是带制定名称的指定枚举类型的枚举常量.

public class EnumTest {
	public static void main(String[] args) {
		Direction direction = Enum.valueOf(Direction.class, "LEFT");
		System.out.println(direction.getName());
		Direction direction2 = Enum.valueOf(Direction.class, "RIGHT");
		System.out.println(direction2.getName());
	}
}
运行结果如下:

left
right
7.枚举类的values()方法,获取的是枚举类的枚举项

public class EnumTest {
	public static void main(String[] args) {
		Direction[] dir = Direction.values();
		for (Direction direction : dir) {
			System.out.println(direction);
		}
	}
}
运行结果如下:

LEFT
TOP
RIGHT
BOTTOM


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值