Java之枚举

Java之枚举

  • enum关键字可以将一组具有名称的值的有限集合创建为一种新的类型——枚举。
  • 枚举的实例是常量,所以命名是名字全部用大小,不同单词之间使用下划线_。
  • 枚举类可以在switch语句中使用。
  • 常用方法:
    • values():返回当前枚举类型中所有枚举常量的数组。
    • valueOf(String name):返回指定名称的枚举的实例,如果不存在则抛异常。
    • name():返回枚举常量的名称。
    • ordinal():返回枚举常量在枚举类型中的顺序。
    • compareTo(Enum o):比较当前枚举常量和指定枚举常量的顺序。
    • equals(Object other):比较当前枚举常量和指定对象是否相等。
    • hashCode():返回当前枚举常量的哈希码值。
    • getDeclaringClass():返回定义该枚举常量的枚举类型。
    • toString():返回枚举常量的字符串表示。它的逆方法是静态方法valueOf()。
    • clone():抛出CloneNotSupportedException异常,枚举常量不能被克隆。
  • 如果想要定义自己的方法,需要在枚举常量序列后面添加一个分号。而且方法和字段必须在枚举常量序列之后。
  • 枚举类型可以像普通的类一样定义构造方法,但是需要注意以下几点:
    • 枚举类型的构造方法默认是private或默认级别,即只能在枚举类型内部调用,不能在其他类中直接实例化枚举类型。
    • 枚举类型中的每个枚举常量都会在枚举类型初始化时被实例化,因此枚举类型的构造方法会在每个枚举常量实例化时调用。
    • 枚举类型的构造方法可以带参数,并可以被枚举常量的实例化语句传递参数。
  •  enum My {
     	S("small"),M("medium"),L("large");
    
     	private String description;
    
     	My(String description) {
         	this.description = description;
     	}
    
     	public String getDescription() {
         	return description;
     	}
    
     	public static void main(String[] args) {
         	for (My my : My.values()) {
             	System.out.println("当前值为" + my);
             	System.out.println("值的含义:" + my.getDescription());
             	System.out.println("值的名称:" + my.name());
             	System.out.println("equals(S):" + my.equals(S));
             	System.out.println("== S:" + (my == S));
             	System.out.println("compareTo(S):" + my.compareTo(S));
             	System.out.println("getDeclaringClass():" + my.getDeclaringClass());
             	System.out.println("---------------------------");
         	}
     	}
     }
     
     输出:
     当前值为S
     值的含义:small
     值的名称:S
     equals(S)true
     == Strue
     compareTo(S)0
     getDeclaringClass()class com.eos.javalearn.My
     ---------------------------
     当前值为M
     值的含义:medium
     值的名称:M
     equals(S)false
     == Sfalse
     compareTo(S)1
     getDeclaringClass()class com.eos.javalearn.My
     ---------------------------
     当前值为L
     值的含义:large
     值的名称:L
     equals(S)false
     == Sfalse
     compareTo(S)2
     getDeclaringClass()class com.eos.javalearn.My
     ---------------------------
    
  • 在switch中使用Enum:
    •  enum Light {
       	RED,GREEN,BLUE;
      
       	public static void test(Light light) {
       		switch (light) {
           		case RED:
               		System.out.println("color is " + RED);
               		break;
           		case BLUE:
               		System.out.println("color is " + BLUE);
               		break;
           		case GREEN:
               		System.out.println("color is " + GREEN);
               		break;
       		}
       	}
      
       	public static void main(String[] args) {
      			test(GREEN);
       	}
       }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值