簡體   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