零基础学Java第十四天之枚举

admin2024-05-15  0

枚举

1、理解

2、定义

3、特点

4、使用

1、案例一季节类

package com.xx.enum02;

public class Test01 {
	/**
	 * 知识点:枚举的引入
	 * 
	 * 需求:编写季节类(Season),该类只有四个对象(spring,summer,autumn,winter)
	 */
	public static void main(String[] args) {
		
		
		System.out.println(Season.spring);
		System.out.println(Season.summer);
		System.out.println(Season.autumn);
		System.out.println(Season.winter);
	}
}

package com.xx.enum02;

//季节枚举类
//注意:枚举就是一个受限制的类(该类只能有固定的几个对象,不能让外界创建对象)
//注意:枚举不能有继承关系(显示继承)
//注意:自定义枚举类隐式继承Enum类 -- public class Season extends Enum{}
public enum Season{
	
	//注意:枚举类的第一行必须声明对象
	
	//spring;//底层实现 - public static final Season spring = new Season();
	//spring();//底层实现 - public static final Season spring = new Season();
	
	spring("春天","春雨绵绵"),//底层实现 - public static final Season spring = new Season("春天","春雨绵绵");
	summer("夏天","烈日炎炎"),//底层实现 - public static final Season spring = new Season("夏天","烈日炎炎");
	autumn("秋天","硕果累      累"),//底层实现 - public static final Season spring = new Season("秋天","硕果累累");
	winter("冬天","白雪皑皑");//底层实现 - public static final Season spring = new Season("冬天","白雪皑皑");
	
	private String name;
	private String info;
	
	//注意:枚举类的构造方法使用private修饰
	private Season() {
	}

	private Season(String name, String info) {
		this.name = name;
		this.info = info;
	}

	public String getName() {
		return name;
	}

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

	public String getInfo() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}
	
	@Override
	public String toString() {
		return name + " -- " + info;
	}
}

2、案例二信号机

package com.xx.enum06;

//信号灯
public enum Signal {

	RED, YELLOW, GREEN;
}

package com.xx.enum06;

import java.util.Scanner;

public class Test01 {
	/**
	 * 知识点:枚举案例 之 状态机
	 */
	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);
		System.out.println("请选择信号灯:RED, YELLOW, GREEN");
		String str = scan.next();

		Signal signal = Signal.valueOf(str);
		String trafficInstruct = getTrafficInstruct(signal);
		System.out.println(trafficInstruct);

		scan.close();
	}

	public static String getTrafficInstruct(Signal signal) {
		String trafficInstruct = "信号灯故障";
		switch (signal) {
		case RED:
			trafficInstruct = "红灯停";
			break;
		case YELLOW:
			trafficInstruct = "黄灯请注意";
			break;
		case GREEN:
			trafficInstruct = "绿灯行";
			break;
		}
		
		return trafficInstruct;
	}
}

3、案例三错误码

package com.xx.enum07;

public enum AddCode {

	ERR01(-1,"添加失败 - 学生信息不合法"),
	ERR02(-2,"添加失败 - 没有该学生"),
	OK(1,"添加成功");
	
	private int code;
	private String message;
	
	private AddCode(int code, String message) {
		this.code = code;
		this.message = message;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	@Override
	public String toString() {
		return code + " -- " + message;
	}
}

package com.xx.enum07;

public class Test01 {
	/**
	 * 知识点:枚举案例 之 错误码
	 */
	public static void main(String[] args) {
		
		System.out.println(AddCode.ERR01);
		System.out.println(AddCode.ERR02);
		System.out.println(AddCode.OK);
		
	}
}

4、案例四 组织枚举

package com.qf.enum08;

public interface Code {

	enum AddCode {

		ERR01(-1,"添加失败 - 学生信息不合法"),
		ERR02(-2,"添加失败 - 有重复学生"),
		OK(1,"添加成功");
		
		private int code;
		private String message;
		
		private AddCode(int code, String message) {
			this.code = code;
			this.message = message;
		}

		public int getCode() {
			return code;
		}

		public void setCode(int code) {
			this.code = code;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}
		
		@Override
		public String toString() {
			return code + " -- " + message;
		}
	}

	enum RemoveCode {

		ERR01(-1,"删除失败 - 学生信息不合法"),
		ERR02(-2,"删除失败 - 没有该学生"),
		OK(1,"删除成功");
		
		private int code;
		private String message;
		
		private RemoveCode(int code, String message) {
			this.code = code;
			this.message = message;
		}

		public int getCode() {
			return code;
		}

		public void setCode(int code) {
			this.code = code;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}
		
		@Override
		public String toString() {
			return code + " -- " + message;
		}
	}

	enum UpdateCode {

		ERR01(-1,"修改失败 - 学生信息不合法"),
		ERR02(-2,"修改失败 - 没有该学生"),
		ERR03(-3,"修改失败 - 修改数据不合法"),
		ERR04(-4,"修改失败 - 目标班级上有学生"),
		ERR05(-5,"修改失败 - 目标学号上有学生"),
		OK(1,"修改成功");
		
		private int code;
		private String message;
		
		private UpdateCode(int code, String message) {
			this.code = code;
			this.message = message;
		}

		public int getCode() {
			return code;
		}

		public void setCode(int code) {
			this.code = code;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}
		
		@Override
		public String toString() {
			return code + " -- " + message;
		}
	}

}

package com.qf.enum08;

public class Test01 {
	/**
	 * 知识点:枚举案例 之 组织枚举
	 * 
	 * 应用场景:可以将一个一个的错误码使用类或接口组织在一起
	 * 
	 * 注意:
	 * 		使用类组织枚举,枚举默认使用static修饰
	 * 		使用接口组织枚举,枚举默认使用public static修饰
	 * 经验:使用接口组织众多枚举
	 */
	public static void main(String[] args) {
		
		System.out.println(Code.AddCode.ERR01);
	}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!