繁体   English   中英

使用可行的帐户的getter和setter函数?

[英]using a getter and setter function viable for account?

我正在做一个涉及创建帐户的程序,我需要创建一个程序,以便它将扫描特定数据以执行分配的命令。 getter和setter函数适合吗?

public class Account {

    //data
    private int userId;
    private String password;
    private char type;

    public Account(int userId, String password, char type) {
        this.userId = userId;
        this.password = password;
        this.type = type;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public char getType() {
        return type;
    }

    public void setType(char type) {
        this.type = type;
    }


    //methods
    public boolean verifyLogin(int usrid , String pass)
    {
      if((usrid == userId) & (pass == password)){
          return true;
      }  
      else{
        return false;
    }
}

您的getter和setter看起来很适合访问此类的数据。

您需要特别注意的是如何检查密码是否正确。

在您的实现中,使用pass == password比较两个字符串。 这是不正确的,您应该使用pass.equals(password)

看起来不错,但是您需要重新考虑是否需要一些值的设置器。 在示例中,用户ID会以某种方式更改不是很常见的用例。 如果要保持持久性,则不需要setter。 在构造函数中设置一次。

另外,您可以查看Lombok项目以及@Getter和@Setter批注。 它将您的代码最小化为3行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM