Java day15【第二十六章】枚举
【第二十六章】枚举
一.枚举的实际应用:
定义一个person类,里面有性别,性别不希望用户随便输入,所以用枚举比较合适。
范例:使用枚举
package Example; enum SEX{ MALE("男"),FEMALE("女"); private String title; private SEX(String tilte){ this.title = title; } public String toString(){ return this.title; } } class Person{ private String name; private int age; private SEX sex; public Person(String name,int age,SEX sex){ this.name = name; this.age = age; this.sex = sex; } public String toString(){ return"姓名:"+this.name+"、年龄:"+this.age+"、性别:"+this.sex; } } public class Enumo { public static void main(String args[]){ System.out.println(new Person("张三",19,SEX.MALE)); } }