JAVA小练习129——自定义一个性别类, 限制着别人任意创建该类的对象(枚举)

enum Gender{
	
	MAN("男"),WOMAN("女");
	
	String value; 
	
	private Gender(String value){
		this.value = value;
	}
}



class Person{
	
	String name;
	
	private Gender sex;
	
	
	
	public Gender getSex() {
		return sex;
	}

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



	@Override
	public String toString() {
		return "姓名:"+ this.name+" 性别:"+ this.sex.value;
	}
}



public class Demo129 {

	public static void main(String[] args) {
	
		Person p = new Person();
		p.name = "狗娃";
		p.setSex(Gender.WOMAN);

		System.out.println(p); 
		
		
	}
}

你可能感兴趣的:(JAVA小练习129——自定义一个性别类, 限制着别人任意创建该类的对象(枚举))