您的位置:首页 > 编程语言 > Java开发

java 基础总结 -- enum 枚举的基础使用

2016-02-10 04:03 621 查看
package com.zghw.base.enumx;

/**
* 枚举类型定义
* @author zghw
*
*/
//使用enum关键字定义一个枚举
public enum Spiciness {
//命名习惯以大写字母如有多个则用下划线连接,使用逗号分开具名值
NOT,MILD,MEDIUM,HOT,FLAMING
}


package com.zghw.base.enumx;
//通过使用import static能够将enum实例的标识符带入当前的命名空间
import static com.zghw.base.enumx.Spiciness.*;
/**
* 枚举的简单使用
* @author zghw
*
*/
public class SimpleEnumUse {
private Spiciness degree;

public SimpleEnumUse(Spiciness degree){
this.degree = degree;
}
/**
* 由于switch取值有限,和enum一起使用是绝配
*/
public void choise(){
System.out.println("choise use");
switch(degree){
case NOT:System.out.println("not ");break;
case HOT:System.out.println("hot");break;
case MILD:
case MEDIUM:
default:System.out.println("defalut");
}
}

public static void main(String args[]){
//使用枚举
Spiciness hot = Spiciness.HOT;
//编译器自动把toString方法输出enum实例名称
System.out.println(hot);
//方法values()产生由这些常量构成的数组
for(Spiciness sp:Spiciness.values()){
//ordinal方法某个特定enum常量的声明排序
System.out.println( sp +" ordinal ="+sp.ordinal());
}
SimpleEnumUse h=new SimpleEnumUse(Spiciness.HOT);
//通过使用import static能够将enum实例的标识符带入当前的命名空间,所以无需像上面
//用enum类型来修饰enum实例
SimpleEnumUse f=new SimpleEnumUse(FLAMING);
SimpleEnumUse m=new SimpleEnumUse(MEDIUM);
h.choise();
f.choise();
m.choise();
}
}


package com.zghw.base.enumx;
/**
* 枚举的基本使用
* @author zghw
*
*/
enum Shrubbery {
GROUND,CRAWLING,HANGING
}
public class EnumClass {

//创建enum时,编译器会为你生成一个相关的类,这个类继承自java.lang.Enum
public static void main(String[] args) {
//调用values()方法,可以遍历enum实例,values方法返回enum实例的数组
//而且该数组中的元素严格保持其在enum中的声明时的顺序,因此你可以在循环中使用values()返回的数组
for(Shrubbery s : Shrubbery.values()){
//ordinal()方法返回enum实例在声明时的次序 从0开始计数
System.out.println(s + ": ordinal =" + s.ordinal());
//enum继承自java.lang.Enum,Enum类实现了Comparable接口,可以用于比较enum实例次序大小
System.out.println(s.compareTo(Shrubbery.CRAWLING));
//可以使用equals或==来比较enum实例
System.out.println(s.equals(Shrubbery.CRAWLING));
System.out.println(s==Shrubbery.CRAWLING);
//通过enum实例的getDeclaringClass()方法,就能知道所属的enum类
System.out.println(s+ "declaring :"+s.getDeclaringClass());
//name()方法返回enum实例声明时的名字,它和toString()方法一样。
System.out.println(s.name());
System.out.println("---------------");
}

for(String s : "HANGING GROUND CRAWLING".split(" ")){
//valueof()方法是Enum中的静态方法,根据对应的字符名称返回对应的enum实例
Shrubbery sh=Enum.valueOf(Shrubbery.class, s);
System.out.println(sh);
}
}

}


/**
* 枚举中使用构造函数 方法 字段 可以使用main方法
* 除了枚举实例外,enum和类的结构基本都一样,可以使用enum做很多事情
* @author zghw
*
*/
public enum OzWitch {
EAST("postion east",System.currentTimeMillis()-11,11),//类似使用构造函数进行enum实例化
WEST("postion west",System.currentTimeMillis()-22,22),//类似使用构造函数进行enum实例化
SOUTH("postion south",System.currentTimeMillis()-33,33),//类似使用构造函数进行enum实例化
NORTH("postion north",System.currentTimeMillis()-44,44);
//如果枚举使用了字段 构造函数 方法等 必须enum实例在前,必须以 ; 结束
private String description;
private Long currentTime;
private int flag;
//枚举中使用

//因为枚举无法实例化 使用私有的构造方法合理
private OzWitch(String description,Long currentTime,int flag){
this.description = description;
this.currentTime = currentTime;
this.flag = flag;
}
//普通方法
public void printInfo(){
System.out.println(this.description +" -- "+this.currentTime+" -- "+this.flag);
}

//使用main方法
public static void main(String args[]){
for(OzWitch o : OzWitch.values()){
o.printInfo();
}
}
}


package com.zghw.base.enumx;

import java.lang.reflect.Method;
import java.util.Set;
import java.util.TreeSet;

import com.zghw.base.OSExecute;

/**
* 使用反射机制分析enum和java.lang.Enum类的关系
* 方法values()的由来
* @author zghw
*
*/
enum Explore {
HERE,THERE
}
public class Reflection {

public static Set<String> analyze(Class<?> enumClass){
System.out.println("Analyze enum class " +enumClass);
//枚举对应的基类
System.out.println(" base class "+enumClass.getSuperclass());
System.out.println(" constain method:");
Set<String> methods = new TreeSet<String>();
for(Method m : enumClass.getMethods()){
methods.add(m.getName());
}
System.out.println(methods);
return methods;
}
public static void main(String[] args) {
Set<String> exploreMethods=Reflection.analyze(Explore.class);
Set<String> enumMethods = Reflection.analyze(Enum.class);
boolean hasContains=exploreMethods.containsAll(enumMethods);
System.out.println("Explore methods contains Enum methods "+hasContains);
exploreMethods.removeAll(enumMethods);
System.out.println("remainder  methods:"+exploreMethods);

//由于values方法是由编译器插入到enum定义中的static方法,所以,如果你将enum实例向上转型为Enum,那么
//values()方法就不可访问了。不过,在Class中有一个getEnumConstants()方法,所以即便Enum接口中
//Enum接口中没有values()方法,可以通过Class对象取得所有的enum实例。
// 转型
Explore[] exs = Explore.values();
Enum enu= Explore.HERE;
//enu.values(); 没有这个方法
for(Enum e:enu.getClass().getEnumConstants()){
System.out.println(e);
}
OSExecute.command("javap /home/zghw/github/java-demo/bin/com/zghw/base/enumx/Explore.class");
/**
* 下面是枚举在编译后生成的类结构
* 枚举生成的类是final 无法继承 所以枚举无法继承
* 枚举的父类是java.lang.Enum 可以使用Enum已有的方法
* 查看java.lang.Enum中并没有values方法说明编译其为每个enum生成了values()方法
* valueOf()方法重载了父类的方法
* 枚举实例 是static final的说明 是一个常量
*
* Compiled from "Reflection.java"
final class com.zghw.base.enumx.Explore extends java.lang.Enum<com.zghw.base.enumx.Explore> {
public static final com.zghw.base.enumx.Explore HERE;
public static final com.zghw.base.enumx.Explore THERE;
static {};
public static com.zghw.base.enumx.Explore[] values();
public static com.zghw.base.enumx.Explore valueOf(java.lang.String);
}
*/
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: