天天看點

Something about enum in Java

Here is a CA enum class:

public enum CA{
  HH,BB,DD;
  public String getString(){
    switch(this){
      case HH:return "HH hello";
      case BB:return "BB hello";
      case DD:return "DD hello";
      default:return "Hello world"
      }
  }
}      

We have another class B:

public class B{
  public CA yc;
}      

I want to show some usages of enum class:

String jsonS = "{"+"\"yc\":\"DD\""+"}";
B b = new Gson().fromJson(jsonS,B.class);
System.out.println(b.getStrting());      

The output is the following:

DD hello      

We can use the String enum to initialize the enum object:

CA bb = CA.valueOf("BB");
System.out.println(bb.getString());      
BB hello