java enum 解析

一、 每一个枚举对象都是java.lang.Enum<E extends Enum<E>>抽象类的子类
    
 举个例子: 策略枚举
1) 代码
package com.javase00001.myenum;
public enum EnumDemo {
	ADD("+") {//每一个常量都是public static final

		@Override
		public int apply(int a, int b) {
			return a + b;
		}
		
	},
	SUB("-") {
		@Override
		public int apply(int a, int b) {
			return a - b;
		}
	},
	MUT("*") {
		@Override
		public int apply(int a, int b) {
			return a * b;
		}
	},
	DIV("/") {
		@Override
		public int apply(int a, int b) throws RuntimeException{
			int result = 0;
			try {
				result = a / b;
			} catch (ArithmeticException e) {
				System.out.println("除数不可以为0");
				throw e;
			}
			return result;
		}
	};
	
	public abstract int apply(int a, int b);
	private EnumDemo(String op) { //只能是私有的
		this.op = op;
	}
	@Override
	public String toString() {
		return this.op;
	}
	// Enum 中final method
/*	@Override
	public boolean equals(Object obj) {
		return true;
	}*/
	private String op;
	
}

2) 测试
@Test
public void enumDemoTest {
		int a, b;
		a = 2;
		b = 1;
		System.out.println(""+ a + EnumDemo.ADD + b + " = "
				+ EnumDemo.ADD.apply(a, b));
		System.out.println(""+a + EnumDemo.DIV + b + " = "
				+ EnumDemo.DIV.apply(a, b));
		System.out.println(""+a + EnumDemo.MUT + b + " = "
				+ EnumDemo.MUT.apply(a, b));
		System.out.println(""+a + EnumDemo.SUB + b + " = "
				+ EnumDemo.SUB.apply(a, b));
}


反编译   javap -v EnumDEmo
Compiled from "EnumDemo.java"
public abstract class com.javase00001.m
yenum.EnumDemo extends java.lang.Enum
  SourceFile: "EnumDemo.java"
  InnerClass:
   #17; //class com/javase00001/myenum/EnumDemo$1
   #28; //class com/javase00001/myenum/EnumDemo$2
   #36; //class com/javase00001/myenum/EnumDemo$3
   #44; //class com/javase00001/myenum/EnumDemo$4
 {
public static final com.javase00001.myenum.EnumDemo ADD;

public static final com.javase00001.myenum.EnumDemo SUB;

public static final com.javase00001.myenum.EnumDemo MUT;

public static final com.javase00001.myenum.EnumDemo DIV;

static {};

public abstract int apply(int, int);

public java.lang.String toString();

public static com.javase00001.myenum.EnumDemo[] values();

public static com.javase00001.myenum.EnumDemo valueOf(java.lang.String);

com.javase00001.myenum.EnumDemo(java.lang.String, int, java.lang.String,        com.javase00001.myenum.EnumDemo);


}



二、问题?
Q:Enum 类中没有 values() : EnumDemo[] 和v alueOf(String)方法?它们从何而来?
A:

In addition, if E is the name of an enum type, then that type has the following implicitly declaredstatic methods:

/**
* Returns an array containing the constants of this enum 
* type, in the order they're declared.  This method may be
* used to iterate over the constants as follows:
*
*    for(E c : E.values())
*        System.out.println(c);
*
* @return an array containing the constants of this enum 
* type, in the order they're declared
*/
public static E[] values();

/**
* Returns the enum constant of this type with the specified
* name.
* The string must match exactly an identifier used to declare
* an enum constant in this type.  (Extraneous whitespace 
* characters are not permitted.)
* 
* @return the enum constant with the specified name
* @throws IllegalArgumentException if this enum type has no
* constant with the specified name
*/
public static E valueOf(String name);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值