Java中的枚举

Java枚举

1.枚举是什么

1.百度百科

在数学和计算机科学理论中,一个集的枚举是列出某些有穷序列集的所有成员的程序,或者是一种特定类型对象的计数。这两种类型经常(但不总是)重叠。

2.LZ解释

  • 枚举是一个类。
  • 枚举隐藏了类型。
  • 枚举中可以有抽象方法。每个枚举元素都要实现抽象方法。
  • 可以继承接口,但是不能继承类。
  • 每一个枚举元素是这个类的实例。只能有抽象方法和私有方法,没有公有方法.
  • 每一个枚举元素都是常量。

2.枚举解决了什么问题

解决了变量有限值的取值范围。

2.返回类型统一规范

枚举的所有元素返回值类型都是一样的比较容易操作。

3.限制了范围

枚举的返回值一定不会超过 其枚举的范围,这样就能避免了变量超出了枚举范围。

3.使用方式

1. 常量

public enum Color {

	RED, GREEN, BLACK, YELLOW;

}

获取方式

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

可以使用在switch中

public class test {

	public static void main(String[] args) {

		showClor(Color.RED);

	}

	private static void showClor(Color color) {
		switch (color) {
		case RED:
			System.out.println(color);
			break;
		case GREEN:
			System.out.println(color);
			break;
		case BLACK:
			System.out.println(color);
			break;
		case YELLOW:
			System.out.println(color);
			break;
		}

	}
}

2.自定义变量和方法

public enum Color {

	RED("red", 1), GREEN("green", 2), BLACK("black", 3), YELLOW("yellow", 4);

	private String name;
	private Integer index;

	private Color(String name, Integer index) {
		this.name = name;
		this.index = index;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

}

使用演示

public static void main(String[] args) {
  // 输出某一枚举的值
  System.out.println(Color.RED.getName());
  // 遍历枚举
  for (Color color : Color.values()) {
    System.out.println(color.getIndex() + "." + color.getName());
  }

}

输出结果

red

1.red

2.green

3.black

4.yellow

3.枚举的公共方法和抽象方法.

上个演示中遍历枚举的结果我们就可以将他装入一个公共方法中

public enum Color {

	RED("red", 1), GREEN("green", 2), BLACK("black", 3), YELLOW("yellow", 4);

	private String name;
	private Integer index;

	private Color(String name, Integer index) {
		this.name = name;
		this.index = index;
	}
	//公共方法
	public void showColor() {
		System.out.println(index + "." + name);
	}


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

}

使用演示

public static void main(String[] args) {
  // 便利枚举
  for (Color color : Color.values()) {
    //System.out.println(color.getIndex() + "." + color.getName());
    color.showColor();
  }
}

添加抽象方法让各个枚举元素自己实现
为了区别我将分隔符差异化设置

public enum Color {

	RED("red", 1) {
		@Override
		public void showColor() {
			System.out.println(RED.getIndex()+"."+RED.getName());

		}
	}, GREEN("green", 2) {
		@Override
		public void showColor() {
			System.out.println(GREEN.getIndex()+"+"+GREEN.getName());

		}
	}, BLACK("black", 3) {
		@Override
		public void showColor() {
			System.out.println(BLACK.getIndex()+"_"+BLACK.getName());

		}
	}, YELLOW("yellow", 4) {
		@Override
		public void showColor() {
			System.out.println(YELLOW.getIndex()+"*"+YELLOW.getName());

		}
	};

	private String name;
	private Integer index;

	private Color(String name, Integer index) {
		this.name = name;
		this.index = index;
	}
	//公共方法
	public abstract void showColor();


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

}

使用演示

public static void main(String[] args) {
  // 便利枚举
  for (Color color : Color.values()) {
    //System.out.println(color.getIndex() + "." + color.getName());
    color.showColor();
  }
}

显示结果

1.red

2+green

3_black

4*yellow

4.其他的枚举用法

(巨坑待填)

4.枚举的缺点

枚举这种类型虽然好处多多,但是枚举使用量比较少。如果没有使用过的人阅读枚举代码会产生困惑。比如LZ百度了半天不得其法然后专门抽时间学习了一下。

5.总结

总体来说枚举类的使用增强了代码的健壮性,构建的数据模板也是可扩展的。能用枚举的地方还是尽量用枚举,来保证自己的取值范围在限定范围之内。

原文地址:https://www.cnblogs.com/yanlong300/p/7992626.html