如何使用枚举?

use*_*844 0 java

我不确定如何在Java中使用枚举.我在以下示例中正确使用它吗?

enum Sex {M, F};

public class person{  
    private person mom;  
    private Sex sex;

    public person(Sex sex){  
        this.sex = sex;  
        //is this how i'd set sex?  
    }  

    public void setMom(person mom){  
        if(mom.sex == 'M'){}  
        //is this how i would check to see if the sex of the passed person argument is male?  
   }  
}
Run Code Online (Sandbox Code Playgroud)

Mic*_*rdt 5

这是你想要使用的:

 if(mom.sex == Sex.M)
Run Code Online (Sandbox Code Playgroud)

但是,我肯定会将枚举常量拼写为MALE和FEMALE.