Java构造函数
再会!
我创建了重载构造函数,如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
首先,DRY(不要重复自己)会更好:
其次,代码中没有增加 2。请粘贴您的测试代码。
First, DRY (do not repeat yourself), would be better:
Second, there is no increment by two in your code. Please paste in your test code.
尝试删除第一组括号
ContactsBean() 联系人 = new ContactsBean();
。也就是说,尝试这个构造函数:
Try removing the first set of parentheses from
ContactsBean() contacts = new ContactsBean();
.That is, try this constructor:
我认为您正在其他类中创建两个 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.
您是否尝试过在两个构造函数上设置断点来调试代码?
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...
这可能是由于复制构造函数调用了构造函数的无参数版本。
(或者我患有C++病?)
It might be due to the copy constructor calling the no-arg version of your contstructor.
(Or am I suffering from C++ sickness?)
我的建议是不要尝试在构造函数中增加 contactID,而是从新创建的数据库对象中获取它,其中 ID 通过身份规范由数据库增加,或者因为您正在获取基于您的下一个 id 的联系人列表关闭 contactDAO.getContactsList().size()+1。
我还建议从: 更改
为:
或者,如果您能够从数据库中删除联系人,则此号码对于 ID 来说可能不准确。您可以基于以下内容创建查询:
这将返回使用的最大 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:
To something like:
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:
This will return the largest id number used.
我了解到 Struts2 在 Action 类中自动生成对象(bean)的实例,因此不需要实例化它......
我之前的代码是
我将其更改为......
并且它可以工作......
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
I changed it to..
And it works...