我正在创建一个包含三个字段的 Employee 类:姓名、年龄和性别。我需要创建一个性别字段,但用户只能选择男性或女性。我想我必须以布尔形式执行此操作,但我不知道该怎么做。谁能帮我吗?
到目前为止,我有:
public class Employee {
private String name;
private int age;
private boolean gender;
private boolean male;
private boolean female;
public Employee(String name, int age, boolean gender)
{
this.name = name;
this.age = age;
boolean f = female;
boolean m = male;
if (gender = f)
{
System.out.print("female");
}
else if (gender = m)
{
System.out.print("male");
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender)
{
if (gender = f)
{
System.out.print("female");
}
else if (gender = m)
{
System.out.print("male");
}
}
}