Java 如何在 getter 和 setter 中使用枚举?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19879846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:59:48  来源:igfitidea点击:

How do I use Enums in getters and setters?

javaenums

提问by user2973447

So what I am trying to do is this:

所以我想要做的是:

Write a User class

编写用户类

A User:

用户:

  • has a username e.g 'fj3'
  • has a userType which can be: 'user', 'editor' or 'admin'
  • has a name e.g 'Francis'
  • has a constructor which takes the username, userType and name as parameters
  • has a getUsername() method
  • has a getUserType() method
  • has a getName() method
  • has a setUserType() method which takes one of the user types as a parameter
  • 有一个用户名,例如“fj3”
  • 有一个 userType 可以是:'user'、'editor' 或 'admin'
  • 有一个名字,例如“弗朗西斯”
  • 有一个以用户名、用户类型和名称作为参数的构造函数
  • 有一个 getUsername() 方法
  • 有一个 getUserType() 方法
  • 有一个 getName() 方法
  • 有一个 setUserType() 方法,它将用户类型之一作为参数

My code so far:

到目前为止我的代码:

public class User{

     public String id;
     public String userPermissions;
     public String actualName;

     public User(String username, String userType, String name){
         id = username;
         userPermissions = userType;
         actualName= name;
     }

    public String getUsername(){
        return id;
    }

    public String getUserType(){
        return userPermissions;
    }       

    public String getName(){
        return actualName;
    }

    public enum UserType{
       admin, editor, user;
    }

    public void setUserType(String input){
        userPermissions = input;
    }
}

What do I need to do to get this to work? I do not know how to make it so the only usertypes that can be chosen are admin, editor or user.

我需要做什么才能让它发挥作用?我不知道怎么做,所以唯一可以选择的用户类型是管理员、编辑器或用户。

采纳答案by kelunik

You have to change your types to this enum:

您必须将类型更改为此枚举:

public class User {
     public enum UserType {
        ADMIN, EDITOR, USER;
     }

     public String id;
     public UserType userPermissions;
     public String actualName;

     public User(String username, UserType userType, String name) {
         id = username;
         userPermissions = userType;
         actualName= name;
     }

    public String getUsername() {
        return id;
    }

    public UserType getUserType() {
        return userPermissions;
    }       

    public String getName() {
        return actualName;
    }

    public void setUserType(UserType input) {
        userPermissions = input;
    }
}

回答by scottb

Because you have already declared an enum type to represent the possible values for userType, you've already gone a long way to solving this problem.

因为您已经声明了一个枚举类型来表示 userType 的可能值,所以您已经在解决这个问题上走了很长一段路。

If you declare a variable of type UserType, then the only possible values must be one of the defined enum constants.

如果声明 UserType 类型的变量,则唯一可能的值必须是定义的枚举常量之一。

To restrict the input to your setPermissions method, all you have to do is change to the following:

要限制对 setPermissions 方法的输入,您只需更改以下内容:

public class User{

    public String id;
    public String userPermissions;
    public String actualName;

    public User(String username, String userType, String name){
        id = username;
        userPermissions = userType;
        actualName = name;
    }

    public enum UserType{
        ADMIN, EDITOR, USER;
    }

    public void setUserType(UserType type){
        userPermissions = type.toString();
    }
}