java 自动生成id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15866028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:06:49  来源:igfitidea点击:

Auto generate id

java

提问by user2248471

I'm trying to auto generate an id for customers, and all i get is 0 every time.

我正在尝试为客户自动生成一个 id,每次我得到的都是 0。

Pretty sure the problem is in public void regCustomer().

很确定问题出在public void regCustomer().

public class User {
private String firstName, gender, age;
String surename;
private int customerID;
private static int idCounter = 1000;
User next;

public User(String fN, String sn, String g, String a) {
    firstName = fN;
    surename = sn;
    gender = g;
    age = a;
    //customerID = cID;
    next = null;
}

public void setCustomerID() {
    customerID = idCounter++;
}

public int getCustomerID() {
    return customerID;
}

public String toString() {
    return customerID + "\t" + surename + "\t" + firstName + "\t" + age
            + "\t" + gender;

}

}

}

In the Windowclass

Window班级

public void regCustomer() {


    //int customerID = 0;//= Integer.parseInt(customerIDField.getText());
    String firstName = firstNameField.getText();
    String surename = surenameField.getText();
    String gender = genderField.getText();
    String age = ageField.getText();

    if (!firstName.equals("") && !surename.equals("") && !gender.equals("")&& !age.equals("")) {
        userA.regCustomer(new User(firstName, surename, gender,age));
        User u = new User(firstName, surename, gender,age);
        u.getCustomerID();
        customerIDField.setText("");
        firstNameField.setText("");
        surenameField.setText("");
        ageField.setText("");
        genderField.setText("");
        firstNameField.requestFocus();
    } else
        JOptionPane.showMessageDialog(this, "Alle felt m? fylles inn");
}

回答by Boris the Spider

You never set the ID and that is why it is zero.

您从未设置过 ID,这就是它为零的原因。

You can use a private static final AtomicIntegerto generate your id sequence; simply read from it in your constructor:

您可以使用 aprivate static final AtomicInteger来生成您的 id 序列;只需在您的构造函数中读取它:

private static AtomicInteger ID_GENERATOR = new AtomicInteger(1000);

public User(String fN, String sn, String g, String a) {
    customerID = ID_GENERATOR.getAndIncrement();
    //rest of constructor
}

You should use an AtmoicIntegeras this is thread safe and the getAndIncrementmethod is atomic. There are not such guarantees for an int.

您应该使用 anAtmoicInteger因为这是线程安全的并且该getAndIncrement方法是原子的。没有这样的保证int

The question that needs to answered is whether these items are persisted in any way, and if so what happens then - id generation will always start from 1000 using this technique.

需要回答的问题是这些项目是否以任何方式持久化,如果是,那么会发生什么 - 使用此技术生成 id 将始终从 1000 开始。

回答by drew moore

Move customerID = idCounter++;to the constructor.

移动customerID = idCounter++;到构造函数。