仅供自己参考。来自thinking in java 书籍案例,结合自己的尝试。主要记录自己的问题。
package com.zhi.learnj;
import com.zhi.learnj2.Gender;
public class EnumClass {
public static void main(String[] args){
// Shrubbery that = Shrubbery.CRAWLING;
// for(Shrubbery s: Shrubbery.values()){
// System.out.println(s.ordinal());
// System.out.println(s.name());
// if(that.equals(s)){
// System.out.println(s.toString());
// }
// }
Gender gender = Gender.M;
System.out.println(gender);
// System.out.println(Enum.valueOf(Shrubbery.class, "JACKIEZHI"));
// System.out.println(Shrubbery.CRAWLING.getClass());
// System.out.println(Shrubbery.CRAWLING.getDeclaringClass());
}
}
/**
* 操作系统的类型
* @author jackiezhi<
[email protected]>
* @date 2015年12月31日
*/
enum SystemType implements IService, Runnable{
MAC("issued by apple"),
WINDOWS("issued by microsoft"),
LINUX("issued by god"),
UNIX("issued by god");
private String desc;
SystemType(String desc){
this.desc = desc;
}
public static void main(String[] args){
for(SystemType st: SystemType.values()){
System.out.println(st);
}
}
@Override
public String toString() {
return "name="+super.toString()+", desc="+desc;
}
@Override
public void service(String username) {
}
@Override
public void run() {
}
}
interface IService{
void service(String username);
}
/**
* 枚举类型除了不能被继承之外,可以看成一个常规的类,可以实现接口
* @author jackiezhi<
[email protected]>
* @date 2015年12月31日
*/
enum Shrubbery implements Runnable{GROUND, CRAWLING, HANGING;
@Override
public void run() {
}}
class Shrubbery2 {
public static Shrubbery2
A =new Shrubbery2("A", 0),
B = new Shrubbery2("B", 1);
/**
* name & ordinal 是所有的Enum 类型共有的,因此放到了Enum 这个抽象类中。
* Enum 同时提供一些共有的方法
*/
private String name;
private int ordinal;
private Shrubbery2(String name, int ordinal){
this.name = name;
this.ordinal = ordinal;
}
//compareTo()
//equals()
//name() & ordinal()
}
//不能继承自枚举类型。