Java构造函数

发布于 2024-11-19 14:41:24 字数 1182 浏览 3 评论 0原文

再会!

我创建了重载构造函数,如下所示:

public ContactsBean(String firstName, String lastName,
                String telNumber, String email) {
    this.id = count;
    count = count + 1;
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.telNumber = telNumber;
}

public ContactsBean() {
    this.id = count;
    count = count + 1;
}

我想自动递增 id,因此我使用了此变量:

    private static int count;   
    private int id;

我的问题是,当我实例化 ContactsBean() contacts = new ContactsBean() 时,id 的值增加 2.. 2,4,6,8...等等

为什么?如何让ID号自动加1?

谢谢。

编辑:

行动:

private ContactsBean contacts = new ContactsBean();
private ContactsManager contactsManager = new ContactsManager();

    public String add() {
        contactsManager.addContacts(contacts);
        return SUCCESS;
    }

经理:

private ContactsDAO contactsDAO = ContactsDAO.getInstance();
private List<ContactsBean> contactsList = contactsDAO.getContactsList(); 

public void addContacts(ContactsBean contact) {
    contactsList.add(contact);
}

Good day!

I created overloading constructors as follows:

public ContactsBean(String firstName, String lastName,
                String telNumber, String email) {
    this.id = count;
    count = count + 1;
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.telNumber = telNumber;
}

public ContactsBean() {
    this.id = count;
    count = count + 1;
}

I want to auto-increment the id so i used this variables:

    private static int count;   
    private int id;

My problem is, when I instantiate the ContactsBean() contacts = new ContactsBean(), the value of id is incremented by 2..
2,4,6,8... etc.

Why? How can I do the auto number of ID increment by 1?

Thank you.

EDIT:

Action:

private ContactsBean contacts = new ContactsBean();
private ContactsManager contactsManager = new ContactsManager();

    public String add() {
        contactsManager.addContacts(contacts);
        return SUCCESS;
    }

Manager:

private ContactsDAO contactsDAO = ContactsDAO.getInstance();
private List<ContactsBean> contactsList = contactsDAO.getContactsList(); 

public void addContacts(ContactsBean contact) {
    contactsList.add(contact);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

吖咩 2024-11-26 14:41:24

首先,DRY(不要重复自己)会更好:

    public ContactsBean(String firstName, String lastName,
                String telNumber, String email){
    this();
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.telNumber = telNumber;
}

其次,代码中没有增加 2。请粘贴您的测试代码。

First, DRY (do not repeat yourself), would be better:

    public ContactsBean(String firstName, String lastName,
                String telNumber, String email){
    this();
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.telNumber = telNumber;
}

Second, there is no increment by two in your code. Please paste in your test code.

叫嚣ゝ 2024-11-26 14:41:24

尝试删除第一组括号
ContactsBean() 联系人 = new ContactsBean();
也就是说,尝试这个构造函数:

ContactsBean contacts = new ContactsBean();

Try removing the first set of parentheses from
ContactsBean() contacts = new ContactsBean();.
That is, try this constructor:

ContactsBean contacts = new ContactsBean();
橘寄 2024-11-26 14:41:24

我认为您正在其他类中创建两个 ContactsBean 对象,但您可能没有意识到这一点。你必须检查代码。

I think you are creating two objects of ContactsBean in your other classes may be you are unaware of it. You have to check the code.

蓦然回首 2024-11-26 14:41:24

您是否尝试过在两个构造函数上设置断点来调试代码?

Eng的建议。 Fouad 是一个很好的建议,但它并不能解决你的问题。

另请注意,您的计数器不是线程安全的(不过,问题与它无关。在这种情况下,您的计数器的值将低于应有的值)

并且如果您确实需要跟踪实际有多少个对象创建,我不认为最好的方法是在 Java Bean 中使用静态属性......

Have you tried debugging your code setting a breakpoint on both constructors?

The suggestion of Eng. Fouad is a good tip but it's not gonna solve your problem.

Also note that your counter is not thread-safe (the problem has nothing to do with it, though. In that case your counter would have a lower value than it should)

And if you really need to keep track on how many objects you actually create, I don't think the best way to do this is with a static attribute in a Java Bean...

浮世清欢 2024-11-26 14:41:24

这可能是由于复制构造函数调用了构造函数的无参数版本。

(或者我患有C++病?)

It might be due to the copy constructor calling the no-arg version of your contstructor.

(Or am I suffering from C++ sickness?)

孤君无依 2024-11-26 14:41:24

我的建议是不要尝试在构造函数中增加 contactID,而是从新创建的数据库对象中获取它,其中 ID 通过身份规范由数据库增加,或者因为您正在获取基于您的下一个 id 的联系人列表关闭 contactDAO.getContactsList().size()+1。

我还建议从: 更改

private ContactsDAO contactsDAO = ContactsDAO.getInstance();
private List<ContactsBean> contactsList = contactsDAO.getContactsList(); 

public void addContacts(ContactsBean contact) {
    contactsList.add(contact);
}

为:

private ContactsDAO contactsDAO = ContactsDAO.getInstance();
private List<ContactsBean> contactsList; 

public void addContacts(ContactsBean contact) {
    int id = getContactList().size()+1;
    contact.setId(id);
    contactsList.add(contact);
}

public List<ContactsBean> getContactList(){
     return contactsDAO.getContactsList();
}

或者,如果您能够从数据库中删除联系人,则此号码对于 ID 来说可能不准确。您可以基于以下内容创建查询:

select MAX(ID) from contacts

这将返回使用的最大 ID 号。

My suggestion would be to not try to increment the contactID in the contructor, but either get it from the newly created database object where the ID is being incremented by databse via identity specification, or since you are getting the list of contacts base your next id off of contactsDAO.getContactsList().size()+1.

I'd also recommend changing from:

private ContactsDAO contactsDAO = ContactsDAO.getInstance();
private List<ContactsBean> contactsList = contactsDAO.getContactsList(); 

public void addContacts(ContactsBean contact) {
    contactsList.add(contact);
}

To something like:

private ContactsDAO contactsDAO = ContactsDAO.getInstance();
private List<ContactsBean> contactsList; 

public void addContacts(ContactsBean contact) {
    int id = getContactList().size()+1;
    contact.setId(id);
    contactsList.add(contact);
}

public List<ContactsBean> getContactList(){
     return contactsDAO.getContactsList();
}

alternatively if you are able to remove contacts from the database, this number might not be accurate for the ID. You could create a query based on such as:

select MAX(ID) from contacts

This will return the largest id number used.

遗弃M 2024-11-26 14:41:24
private static int count = 0;
private int id;
// ...
public ContactsBean(String firstName, String lastName,String telNumber, String email)
{
    this();
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.telNumber = telNumber;
}
public ContactsBean()
{
    id = ++count;
}
private static int count = 0;
private int id;
// ...
public ContactsBean(String firstName, String lastName,String telNumber, String email)
{
    this();
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.telNumber = telNumber;
}
public ContactsBean()
{
    id = ++count;
}
她如夕阳 2024-11-26 14:41:24

我了解到 Struts2 在 Action 类中自动生成对象(bean)的实例,因此不需要实例化它......

我之前的代码是

private ContactsBean contacts = new ContactsBean();
private ContactsManager contactsManager = new ContactsManager();

    public String add() {
        contactsManager.addContacts(contacts);
        return SUCCESS;
    }
    //getters and setters

我将其更改为......

private ContactsBean contacts;
private ContactsManager contactsManager = new ContactsManager();

    public String add() {
        contactsManager.addContacts(contacts);
        return SUCCESS;
    }
//getters and setters

并且它可以工作......

I learned that Struts2 generates the instance of an object(beans) automatically in the Action Classes so no need to instantiate it....

My code before is

private ContactsBean contacts = new ContactsBean();
private ContactsManager contactsManager = new ContactsManager();

    public String add() {
        contactsManager.addContacts(contacts);
        return SUCCESS;
    }
    //getters and setters

I changed it to..

private ContactsBean contacts;
private ContactsManager contactsManager = new ContactsManager();

    public String add() {
        contactsManager.addContacts(contacts);
        return SUCCESS;
    }
//getters and setters

And it works...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文