1,枚举的 作用:
- 限制数据的值,避免造成非法数据的传入;
-
2,枚举的 格式:
public enum枚举名 {
成员变量名1,成员变量名2,成员变量名3;
}
这里的成员变量名,能用中文。但是枚举之外的变量就不建议了。
枚举中的成员变量名也称为”枚举项”//枚举public enum Sexset {//变量。man,women;}//Person类public class Person {//这里直接定义枚举类型的变量 sexpublic Sexset sex;public Person() {}public Person(Sexset sex) {this.sex = sex;}public void showSex(){System.out.println(sex);}}//主类:public class MeijU {public static void main(String[] args) {//将枚举的变量传给构造器。Person person = new Person(Sexset.man);person.showSex();}}
3,枚举的应用场景:
需要用到固定值的情况下,才使用枚举,如:性别,小时,等;
