Java Development often uses a limited number of constants, which can be defined within the bean as an enum type, so in the maintenance of more convenient to control, so here is a simple record of the definition and usage of enum, convenient for future direct reference use, lazy people plan, hehe ... OK, just look at the code ...
1, java bean definition containing enum type
public class Xxxbean {//other bean variable,... Province ...///other Bean variable Getter,setter method,... Province .../** * display status, enum enum type definition/public enum showstatus{/** * 0, do not show/Hide (0, "Do not Show"),/** * 1,
Show * * (1, "show"); Private Integer Code; Corresponds to the first value in parentheses, that is, 0 or 1 private String description; Corresponding to the second value in parentheses, that is, do not display or display//if there are other defined values in parentheses, you can define the corresponding variable corresponding to it//the Getter,setter method of each variable, note that this is a public-common modifier.
GetCode () {return code;
public void Setcode (Integer code) {this.code = code;
Public String GetDescription () {return description;
} public void SetDescription (String description) {this.description = description;
//enum construct method, note that this is a private, proprietary modifier, private showstatus (Integer code, String description) {this.code = code;
this.description = description;
//Through code, that is, 0 or 1 to get the corresponding Enum object public static Showstatus Getbycode (Integer code) {Showstatus showstatus = null; for (int i=0; I<showstatus.values (). lengtH
i++) {showstatus = Showstatus.values () [i];
if (Code = = Showstatus.getcode ()) {break;
} return showstatus;
}
}
}
use of 2,enum types
Gets the entire enum object of the Showstatus type of Hide
showstatus v1 = xxxbean.hide;
Gets the hide, which is the name of the Enum object in Hide
string v2 = XXXBean.Hide.name ();//This is the default method
//Gets the code value of the enum object of 0, which is the Hide: 0
int v3 = XXXBean.Hide.getCode ();
Gets the hidden, that is, the description value of the Enum object for hide: Hidden
Java enum enum type is basically defined and used, this is the most commonly used, of course, there are other uses, welcome to the brick ...