01 25Java中级之枚举

1 定义枚举类

JDK 1.5之后出现枚举,在实际的开发之中,枚举的作用是用于定义有限个数对象的一种结构(多例设计模式)。枚举就属于多例设计,并且其结构要比多例设计更加的简洁。

在程序之中提供有enum的关键字,利用此关键字可以实现枚举的定义。
范例:定义一个枚举

enum Color{
	RED, GREEN, BLUE;
}

public class JavaDemo{

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

枚举和多例设计模式可以实现同样的需求,但是枚举在编译时可以检查出错误的请求,而多例设计模式只能在运行时出现异常。另外,在进行枚举处理的时候,还可以利用values()方法获取所有的枚举对象进行输出。
范例:获取所有的枚举对象

enum Color{
	RED, GREEN, BLUE;
}

public class JavaDemo{

	public static void main(String[] args){
		for(Color temp : Color.values()){
			System.out.println(temp);
		}
	}
}

从JDK 1.5追加了枚举结构之后,,就可以在switch之中进行枚举项的判断。
范例:观察枚举与switch处理

enum Color{
	RED, GREEN, BLUE;
}

public class JavaDemo{

	public static void main(String[] args){
		Color co = Color.RED;
		switch(co){
			case RED: System.out.println("red");break;
			case GREEN: System.out.println("red");break;
			case BLUE: System.out.println("red");break;
			default: break;
		}

	}
}

2 Enum类

严格意义上来讲枚举并不是一种新的结构,它的本质相当于一个类,但是这个类默认会继承Enum类,Enum类的定义如下:

public abstract class Enum<E extends Enum<E>>
extends Object
implements Comparable<E>, Serializable

现在定义的枚举类的类型就是Enum中所使用的E类型。以下是Enum类的部分方法:

No. 方法名称 类型 方法描述
01 protected Enum​(String name, int ordinal) 构造 传入名字和序号
02 public final String name() 普通 获得对象的名字
03 public final int ordinal() 普通 或的对象序号

范例:观察Enum类的存在

enum Color{
	RED, GREEN, BLUE;
}

public class JavaDemo{

	public static void main(String[] args){
		Color co = Color.RED;
		for(Color temp : Color.values()){
			System.out.println( temp.ordinal() + " - " + temp.name() );
		}

	}
}

在枚举之中每一个对象的序号都是根据枚举对象的定义顺序来决定的。
面试题:请解释enum和Enum的区别?
(1)enum:是从JDK 1.5之后提供的一个关键字,用于定义枚举类;
(2)Enum:是一个抽象类,所以使用enum关键字定义的类就默认继承了此类。

3 定义枚举结构

枚举本身就属于多例设计模式,既然是多例设计模式,那么在一个类之中可以定义的结构是非常多。例如:构造方法、普通方法、属性等,那么这些内容在枚举类中依然可以直接定义,但是需要注意的是:枚举类中定义的构造方法不能采用非私有化定义(public无法使用)。
范例:在枚举类中定义其他结构

enum Color{
	RED("红色"), GREEN("绿色"), BLUE("蓝色"); //枚举类型一定要放在首行
	private String title;  //定义属性

	private Color(String title){
		this.title = title;
	}

	public String toString(){
		return this.title;
	}
}

public class JavaDemo{

	public static void main(String[] args){
		Color co = Color.RED;
		for(Color temp : Color.values()){
			System.out.println( temp.ordinal() + " - " + temp.name() + " - " + temp);
		}

	}
}

本程序在简化程度上远远高于多例设计模式。除了这种基本的结构之外,在枚举类中也可以实现接口的继承。
范例:让枚举实现接口

interface IMessage{
	public abstract String getMessage();
}

enum Color implements IMessage{
	RED, GREEN, BLUE; //枚举类型一定要放在首行

	public String getMessage(){
		return this.toString();
	}
}

public class JavaDemo{

	public static void main(String[] args){
		Color co = Color.RED;
		for(Color temp : Color.values()){
			System.out.println( temp.ordinal() + " - " + temp.name() + " - " + temp.getMessage());
		}

	}
}

在枚举类里面可以直接定义抽象方法,并且要求每一个枚举对象都要独立覆写此抽象方法。
范例:观察枚举中定义抽象方法

enum Color{
	RED{
		public String getInfo(){
			return this.toString();
		}
	}, GREEN{
		public String getInfo(){
			return this.toString();
		}
	}, BLUE{
		public String getInfo(){
			return this.toString();
		}
	}; //枚举类型一定要放在首行

	public abstract String getInfo();
}

public class JavaDemo{

	public static void main(String[] args){
		Color co = Color.RED;
		for(Color temp : Color.values()){
			System.out.println( temp.getInfo());
		}

	}
}

枚举正确用法:定义一个实例对象即可。

4 枚举应用案例

现在定义一个Person类,里面一定有性别,性别肯定不希望用户随意输入,所以使用枚举最合适。
范例:使用枚举

enum Sex{
	MALE("男"), FEMALE("女");
	private String title;

	private Sex(String title){
		this.title = title;
	}

	public String toString(){
		return this.title;
	}
}

class Person{
	private String name;
	private int age;
	private Sex sex;

	public Person(){}

	public Person(String name, int age, Sex sex){
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public String toString(){
		return this.name + "  " + this.age + "  " + this.sex; 
	}
}

public class JavaDemo{

	public static void main(String[] args){
		Person per = new Person("lks", 23, Sex.MALE);
		System.out.println(per);
	}
}

发布了77 篇原创文章 · 获赞 11 · 访问量 2647

猜你喜欢

转载自blog.csdn.net/weixin_43762330/article/details/104564398