我试图理解Java中的枚举,但目前找不到解释我问的问题的帖子.
我有一个Person
界面和一个PersonEO
类.Person接口的两种方法是setGender和getGender.我想把性别作为一个枚举.
我是否在界面中创建枚举并将其转移到课程中,或者在课堂上创建枚举?
这是我一直在尝试的但是无法让enum正确地传递给类,并且不能真正理解它将如何流经程序.
public interface Person{ public enum gender{ Female, Male, Unknown; private String gender; public String getGender(){ } void setGender(){ } } ------------------------------------------------------ public class PersonEO implements Person { //Both override methods are not seen as methods from the interface @Override public String getGender() { return gender; } @Override public void setGender() { //Not sure how to set the gender from the enum this.gender = gender; } }
请举例说明.
枚举,接口和类都是单独的类型,通常应该在单独的文件中.以下是一些示例定义:
enum Gender { MALE, FEMALE, UNKNOWN; } interface Person { void setGender(Gender gender); Gender getGender(); } class PersonImpl implements Person { private Gender gender; @Override public void setGender(Gender gender) { this.gender = gender; } @Override public Gender getGender() { return this.gender; } }