这里介绍一下java中关于枚举的使用。
java中枚举的使用
一、枚举中可以定义方法
参照于TimeUnit的使用,TimeUnit.MILLISECONDS.sleep(1000);
- LoveUtils的类:
package com.linux.huhx.enumTest; /** * Created by huhx on 2017-05-24. */ public enum LoveUtils { BOYS { public String getName() {return "huhx";} public String getPassword() {return "123456";} }, GRIL { public String getName() {return "linux";} public String getPassword() {return "654321";} }; public String getName() { throw new AbstractMethodError(); } public String getPassword() { throw new AbstractMethodError(); } public void sayHello() { String name = getName(); System.out.println("my name is " + name + ", and password: " + getPassword()); } }
- 测试的EnumTest1类代码:
package com.linux.huhx.enumTest; /** * Created by huhx on 2017-05-24. */ public class EnumTest1 { public static void main(String[] args) { LoveUtils.BOYS.sayHello(); // my name is huhx, and password: 123456 LoveUtils.GRIL.sayHello(); // my name is linux, and password: 654321 } }