简体   繁体   English

在我的主应用程序中调用setter方法

[英]calling setter methods in my main application

package com.intermedix.domain;

public class Login {
      public Login(){}
        private String username;
        private String password;

        public String getPassword() {
            return password;
        }

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

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;

        }
}

Can i call the setUsername and setPassword in my main application like this. 我可以这样在我的主应用程序中调用setUsername和setPassword吗?

Login lg = new Login();
lg.setUsername("somename");
lg.setPassword("somepassword");

Is this the right way of doing. 这是正确的做法吗?

Yes that will work, alternatively you could provide a constructor that allows the username and password to be set when creating the Login object: 是的,它将起作用,或者,您可以提供一个构造函数,该构造函数允许在创建Login对象时设置用户名和密码:

public Login(String username, String password)
{
    this.username = username;
    this.password = password;
}

and created thus: 并因此创建:

Login lg = new Login("username", "password");

Yes it's the right way. 是的,这是正确的方法。

Why didn't you try to compile before asking? 您为什么不尝试在询问之前进行编译?

Why not? 为什么不? You can always call setters of your beans if you need it. 如果需要,您始终可以调用bean的安装程序。 It is probably strange that user and password are hard coded in your example, but probably it is an example only. 在您的示例中,用户名和密码是硬编码的,这可能很奇怪,但可能仅是一个示例。

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

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