简体   繁体   English

Java 构造函数

[英]Java Constructor

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:我想自动增加 id 所以我使用了这个变量:

    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.我的问题是,当我实例化ContactsBean() contacts = new ContactsBean()时,id 的值增加了 2.. 2,4,6,8... 等等。

Why?为什么? How can I do the auto number of ID increment by 1?如何使 ID 的自动编号增加 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);
}

First, DRY (do not repeat yourself), would be better:首先,DRY(不要重复自己),会更好:

    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.其次,您的代码中没有增加 2。 Please paste in your test code.请粘贴您的测试代码。

Try removing the first set of parentheses from ContactsBean() contacts = new ContactsBean();尝试从ContactsBean() contacts = new ContactsBean();中删除第一组括号. . That is, try this constructor:也就是说,试试这个构造函数:

ContactsBean contacts = new ContactsBean();

I think you are creating two objects of ContactsBean in your other classes may be you are unaware of it.我认为您在其他类中创建了两个 ContactsBean 对象,您可能不知道。 You have to check the code.你必须检查代码。

Have you tried debugging your code setting a breakpoint on both constructors?您是否尝试过调试在两个构造函数上设置断点的代码?

The suggestion of Eng. Eng的建议。 Fouad is a good tip but it's not gonna solve your problem. Fouad 是一个很好的提示,但它不会解决您的问题。

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...而且,如果您确实需要跟踪实际创建的对象数量,我认为最好的方法不是使用 Java Bean 中的 static 属性...

It might be due to the copy constructor calling the no-arg version of your contstructor.这可能是由于复制构造函数调用了构造函数的无参数版本。

(Or am I suffering from C++ sickness?) (或者我是否患有 C++ 病?)

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.我的建议是不要尝试增加构造函数中的contactID,而是从新创建的数据库 object 中获取它,其中 ID 由数据库通过身份规范递增,或者因为您正在获取基于您的下一个 id 的联系人列表关闭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.或者,如果您能够从数据库中删除联系人,则此数字可能与 ID 不准确。 You could create a query based on such as:您可以基于以下内容创建查询:

select MAX(ID) from contacts

This will return the largest id number used.这将返回使用的最大 ID 号。

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;
}

I learned that Struts2 generates the instance of an object(beans) automatically in the Action Classes so no need to instantiate it....我了解到 Struts2 在动作类中自动生成对象(bean)的实例,因此无需实例化它......

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...它有效...

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

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