1. 枚举
1. 枚举
枚举是一种受限制的类,枚举第一行必须创建对象,枚举不能显示继承父类(枚举没有父类),所有的自定义枚举类默认继承Enum类
package com.wz.enum01;
public enum Season {
spring("春天","春雨绵绵"),
summer("夏天","烈日炎炎"),
autumn("秋天","硕果累累"),
winter("冬天","寒风凌冽")
;
private String name;
private String info;
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 this.name+":"+this.info;
}
}
package com.wz.enum01;
public class test01 {
/**
* 知识点:枚举
* @param args
*/
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);
}
}
2. 枚举的特点
- 枚举就是一个受限制的类,默认继承Enum
- 枚举的第一行必须定义该枚举类型的对象
- 枚举类型对象默认添加: public static final 类型
- 枚举没有继承明确类(自定义枚举类默认继承Enum,Enum默认继承Object)
- 枚举类不能被继承
- 枚举里可以有构造方法、成员方法、静态方法、抽象方法
- 枚举可以实现接口
- 枚举里没有定义方法,可以在最后一个对象后面加逗号、分号或什么都不加
3. 枚举的优势
- 增强代码可读性
- 枚举型可直接与数据库交互
- switch语句优势
- 编译优势
(枚举类编译时,没有把常量值编译到代码中,即使常量值发生改变,也不会影响引用常量的类 )
- 将常量组织起来,统一管理
- 去除equals两者判断 由于常量值地址唯一,使用枚举可以直接通过“==”进行两个值之间的对比,性能会有所提高
2.枚举方法
1. 枚举方法
方法名 | 解释 |
Enum.valueOf(Class enumType, String name) | 根据字符串找到该枚举类中的对象 |
public static void values() | 获取该枚举类对象数组 |
public static void valueOf(String args0) | 根据字符串获取该枚举类中的对象 |
public final String name() | 获取该枚举对象名字 |
public final Class getDeclaringClass() | 获取枚举对象的枚举类型相对应的Class对象 |
public final int hashCode() | 获取该枚举对象的hash值 |
public final int compareTo(E o) | 两个枚举对象进行比较 |
public final boolean equals(Object other) | 比较两个枚举对象是否相同 |
package com.wz.enum01;
public class test02 {
/**
* 知识点:枚举的方法
*
*/
public static void main(String[] args) {
//根据字符串找到该枚举类中的对象
Season season1 = Enum.valueOf(Season.class, "spring");
System.out.println(season1);
//根据字符串获取该枚举类中的对象
Season season2 = Season.valueOf("spring");
System.out.println(season2);
System.out.println(season1==season2);
//获取该枚举队对象的名字
Season[] seasons = Season.values();
for (Season season:seasons
) {
System.out.println(season.name());
}
}
}
2. 枚举底层
public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializable {
//枚举对象名
private final String name;
//枚举编号 -- 从0开始
private final int ordinal;
//获取枚举对象名
public final String name() {
return name;
}
//获取枚举编号
public final int ordinal() {
return ordinal;
}
protected Enum(String name, int ordinal) {
this.name = name;
this.ordinal = ordinal;
}
}
package com.qf.enum02;
//枚举是一个特殊的类,默认继承Enum
public final class Season extends Enum{
//该类的对象引用
public static final Season spring;
public static final Season summer;
public static final Season autumn;
public static final Season winter;
//该类的属性
private String name;//成员属性
private String info;//成员属性
private static final Season[] ENUM$VALUES;//枚举对象数组
//public Season(){}
private Season(String s, int i){
super(s, i);
}
//public Season(String name,String info){}
private Season(String s, int i, String name, String info)
{
super(s, i);
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;
}
public String toString()
{
return (new StringBuilder(String.valueOf(name))).append(" -- ").append(info).toString();
}
//获取该枚举类所有的对象
public static Season[] values()
{
Season aseason[];
int i;
Season aseason1[];
System.arraycopy(aseason = ENUM$VALUES, 0, aseason1 = new Season[i = aseason.length], 0, i);
//思路:创建新数组,将ENUM$VALUES中所有的元素复制到新数组中,返回新数组
//设计思想:不把ENUM$VALUES暴露给外界,因为怕外界修改数组中的元素,所以创建新数组复制并返回
//aseason = ENUM$VALUES;
//int i = aseason.length;
//Season aseason1[] = new Season[i];
//System.arraycopy(aseason, 0, aseason1, 0, i);
return aseason1;
}
public static Season valueOf(String s)
{
return (Season)Enum.valueOf(com/qf/enum02/Season, s);
}
//静态代码块 -- 目的:枚举类的初始化工作
static {
//创建枚举对象(枚举名,对象编号,自定义属性,自定义属性)
//枚举名对象编号的数据传入父类中 -- super(s,i)
spring = new Season("spring", 0, "春天", "春雨绵绵");
summer = new Season("summer", 1, "夏天", "烈日炎炎");
autumn = new Season("autumn", 2, "秋天", "硕果累累");
winter = new Season("winter", 3, "冬天", "寒风凛凛");
//初始化枚举对象数组
ENUM$VALUES = (new Season[] {
spring, summer, autumn, winter
});
}
}
3. 枚举的switch实现
创建数组,获取枚举对象的int值
package com.qf.enum02;
import java.io.PrintStream;
public class Test03{
//[1,2,3,4]
private static int $SWITCH_TABLE$com$qf$enum02$Season[];
public static void main(String args[]){
//switch底层处理枚举类型是获取到枚举类对象对应的int值
switch ($SWITCH_TABLE$com$qf$enum02$Season()[Season.spring.ordinal()]){
case 1:
System.out.println("春天");
break;
case 2:
System.out.println("夏天");
break;
case 3:
System.out.println("秋天");
break;
case 4:
System.out.println("冬天");
break;
default:
System.out.println("默认");
break;
}
}
//初始化$SWITCH_TABLE$com$qf$enum02$Season数组 - 获取到枚举对象在switch的编号
//注意1:枚举对象在枚举类中的编号是从0开始
//注意2:枚举对象在switch中的编号从1开始 -- 思想:第几个枚举对象
static int[] $SWITCH_TABLE$com$qf$enum02$Season(){
//创建int数组 -- new int[4]
int ai[] = new int[Season.values().length];
try
{
//ai[获取枚举对象的编号]
//ai[2] = 3;
ai[Season.autumn.ordinal()] = 3;
}
catch (NoSuchFieldError ) { }
try
{
//ai[0] = 1;
ai[Season.spring.ordinal()] = 1;
}
catch (NoSuchFieldError ) { }
try
{
//ai[1] = 2;
ai[Season.summer.ordinal()] = 2;
}
catch (NoSuchFieldError ) { }
try
{
//ai[3] = 4;
ai[Season.winter.ordinal()] = 4;
}
catch (NoSuchFieldError ) { }
return $SWITCH_TABLE$com$qf$enum02$Season = ai;
}
}
标签:info,String,Season,name,枚举,2023,public,底层
From: https://blog.51cto.com/u_15098423/6411484