Java八大常用类(七) 枚举

2023-02-14 14:00:13 浏览数 (1)

常用类 8 枚举 定义枚举要使用enum 关键字

用来限制一些关键信息

package oop2.Mathh;

public class Student {

代码语言:javascript复制
private String name;
private Gender sex;

public Student(String name , Gender sex){
    this.name = name;
    this.sex = sex;
}

public Student (){}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Gender getSex() {
    return sex;
}

public void setSex(Gender sex) {
    this.sex = sex;
}

@Override
public String toString() {
    return "Student{"  
            "name='"   name   '''  
            ", sex='"   sex   '''  
            '}';
}

} package oop2.Mathh;

public enum Gender {

代码语言:javascript复制
男,女

} package oop2.Mathh;

public class TestA {

代码语言:javascript复制
public static void main(String[] args) {
    Student student = new Student("zhangs",Gender.男);     //在这里的sex就只能选择Gender里的枚举的内容
}

}

0 人点赞