如何将枚举类型添加到具有正常实例变量的类?

时间:2013-01-19 17:52:29

标签: java enums

public class Car
{

    public enum User { "user", "computer", "na" };

    private char car;
    private final User user;
    private boolean call;

    public Car(char object, User user, boolean call)
    {
        this.car = car;
        this.user = user;
        this.call = call;
    }

        public User getUser()
        {
        return user;
        }

    public void setUser(User user)
    {
        this.user = user;
    }

}

当我尝试编译时,我得到以下内容:

错误:预期

错误:',','}'或';'预期

错误:'}'预计

3 个答案:

答案 0 :(得分:5)

不要使用引号:

public enum User { user, computer, na };

答案 1 :(得分:2)

您可以使用:

private enum User { USER, COMPUTER, NA }

另外,要允许分配,请从final声明中删除user关键字:

private User user;

要创建新的Car,您可以使用:

new Car('A', User.COMPUTER, true);

答案 2 :(得分:1)

问题是双引号:

public enum User { "user", "computer", "na" };

删除它们:

public enum User { user, computer, na };