为java中的每个对象生成一个ID
我在主类中有一个数组,它保存Employee类对象。我试图为每个对象生成一个唯一的ID,但它正在为所有对象打印相同的ID 主要类别:为java中的每个对象生成一个ID,java,Java,我在主类中有一个数组,它保存Employee类对象。我试图为每个对象生成一个唯一的ID,但它正在为所有对象打印相同的ID 主要类别: public static void main(String[] args) { Employee employee1 = new Employee("Luke", 36); Employee employee2 = new Employee("Martin", 49); Employee emplo
public static void main(String[] args) {
Employee employee1 = new Employee("Luke", 36);
Employee employee2 = new Employee("Martin", 49);
Employee employee3 = new Employee("Kevin", 21);
Employee employee4 = new Employee("Sam", 43);
Employee employee5 = new Employee("Nicole", 45);
Employee employee6 = new Employee("Linta", 21);
Employee[] allEmployees = { employee1, employee2, employee3, employee4, employee5, employee6 };
for (int i = 0; i < allEmployees.length; i++) {
System.out.println(allEmployees[i]);
}
}
我希望employeeID作为字符串,但不能使用java.util.UUID;对于我的项目。您需要一个与类关联的静态变量来维护唯一的id,并需要一个实例变量来在类中保留特定员工的id
private String employeeID; // instance member
private static String uniqueID = "0"; // static class variable
然后在Employee
构造函数中,使用静态成员:
Employee(String name, int age) {
this.name = name;
this.age = age;
employeeID = Employee.getNextUniqueID();
}
更新的员工类别:
public class Employee {
private String name;
private int age;
private String employeeID;
private static String uniqueID = "0";
Employee(String name, int age) {
this.name = name;
this.age = age;
employeeID = Employee.getNextUniqueID();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static String getNextUniqueID() {
int id = Integer.parseInt(uniqueID);
++id;
uniqueID = Integer.toString(id);
return uniqueID;
}
public String getEmployeeID() {
return employeeID;
}
public String toString() {
return getName() + " " + getAge() + " " + getEmployeeID();
}
}
输出:
路加福音361马丁492
凯文21 3
山姆434
妮可455
林塔216
您需要一个与类关联的静态变量来维护唯一id,并需要一个实例变量来在类中保留特定员工的id
private String employeeID; // instance member
private static String uniqueID = "0"; // static class variable
然后在Employee
构造函数中,使用静态成员:
Employee(String name, int age) {
this.name = name;
this.age = age;
employeeID = Employee.getNextUniqueID();
}
更新的员工类别:
public class Employee {
private String name;
private int age;
private String employeeID;
private static String uniqueID = "0";
Employee(String name, int age) {
this.name = name;
this.age = age;
employeeID = Employee.getNextUniqueID();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static String getNextUniqueID() {
int id = Integer.parseInt(uniqueID);
++id;
uniqueID = Integer.toString(id);
return uniqueID;
}
public String getEmployeeID() {
return employeeID;
}
public String toString() {
return getName() + " " + getAge() + " " + getEmployeeID();
}
}
输出:
路加福音361马丁492
凯文21 3
山姆434
妮可455
林塔216
您应该将最后生成的id存储在静态字段中,但对某些员工的id使用非静态 此外,您还应该使用AtomicInteger类型来实现线程安全,您可以将其转换为字符串。检查:
import java.util.concurrent.atomic.AtomicInteger;
public class Employee {
private String employeeID;
private String name;
private int age;
private static AtomicInteger lastGeneratedId = new AtomicInteger(0);
Employee(String name, int age) {
this.name = name;
this.age = age;
employeeID = getNextUniqueID();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static String getNextUniqueID() {
return String.valueOf(lastGeneratedId.incrementAndGet());
}
public String getEmployeeID() {
return employeeID;
}
public String toString() {
return getName() + " " + getAge() + " " + getEmployeeID();
}
public static void main(String[] args) {
Employee employee1 = new Employee("Luke", 36);
Employee employee2 = new Employee("Martin", 49);
Employee employee3 = new Employee("Kevin", 21);
Employee employee4 = new Employee("Sam", 43);
Employee employee5 = new Employee("Nicole", 45);
Employee employee6 = new Employee("Linta", 21);
Employee[] allEmployees = { employee1, employee2, employee3, employee4, employee5, employee6 };
for (int i = 0; i < allEmployees.length; i++) {
System.out.println(allEmployees[i]);
}
}
}
导入java.util.concurrent.AtomicInteger;
公营雇员{
私有字符串employeeID;
私有字符串名称;
私人互联网;
私有静态AtomicInteger lastGeneratedId=新的AtomicInteger(0);
Employee(字符串名称,整数年龄){
this.name=名称;
这个。年龄=年龄;
employeeID=getNextUniqueID();
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共整数getAge(){
回归年龄;
}
公共无效设置(整数){
这个。年龄=年龄;
}
公共静态字符串getNextUniqueID(){
返回字符串.valueOf(lastGeneratedId.incrementAndGet());
}
公共字符串getEmployeeID(){
返回员工ID;
}
公共字符串toString(){
返回getName()+“”+getAge()+“”+getEmployeeID();
}
公共静态void main(字符串[]args){
雇员1=新雇员(“卢克”,36);
雇员2=新雇员(“马丁”,49岁);
雇员雇员3=新雇员(“Kevin”,21);
雇员4=新雇员(“Sam”,43);
雇员5=新雇员(“Nicole”,45岁);
雇员雇员6=新雇员(“Linta”,21);
雇员[]所有雇员={employee1,employee2,employee3,employee4,employee5,employee6};
for(int i=0;i
您应该在静态字段中存储最后生成的id,但对某些员工的id使用非静态
此外,您还应该使用AtomicInteger类型来实现线程安全,您可以将其转换为字符串。检查:
import java.util.concurrent.atomic.AtomicInteger;
public class Employee {
private String employeeID;
private String name;
private int age;
private static AtomicInteger lastGeneratedId = new AtomicInteger(0);
Employee(String name, int age) {
this.name = name;
this.age = age;
employeeID = getNextUniqueID();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static String getNextUniqueID() {
return String.valueOf(lastGeneratedId.incrementAndGet());
}
public String getEmployeeID() {
return employeeID;
}
public String toString() {
return getName() + " " + getAge() + " " + getEmployeeID();
}
public static void main(String[] args) {
Employee employee1 = new Employee("Luke", 36);
Employee employee2 = new Employee("Martin", 49);
Employee employee3 = new Employee("Kevin", 21);
Employee employee4 = new Employee("Sam", 43);
Employee employee5 = new Employee("Nicole", 45);
Employee employee6 = new Employee("Linta", 21);
Employee[] allEmployees = { employee1, employee2, employee3, employee4, employee5, employee6 };
for (int i = 0; i < allEmployees.length; i++) {
System.out.println(allEmployees[i]);
}
}
}
导入java.util.concurrent.AtomicInteger;
公营雇员{
私有字符串employeeID;
私有字符串名称;
私人互联网;
私有静态AtomicInteger lastGeneratedId=新的AtomicInteger(0);
Employee(字符串名称,整数年龄){
this.name=名称;
这个。年龄=年龄;
employeeID=getNextUniqueID();
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共整数getAge(){
回归年龄;
}
公共无效设置(整数){
这个。年龄=年龄;
}
公共静态字符串getNextUniqueID(){
返回字符串.valueOf(lastGeneratedId.incrementAndGet());
}
公共字符串getEmployeeID(){
返回员工ID;
}
公共字符串toString(){
返回getName()+“”+getAge()+“”+getEmployeeID();
}
公共静态void main(字符串[]args){
雇员1=新雇员(“卢克”,36);
雇员2=新雇员(“马丁”,49岁);
雇员雇员3=新雇员(“Kevin”,21);
雇员4=新雇员(“Sam”,43);
雇员5=新雇员(“Nicole”,45岁);
雇员雇员6=新雇员(“Linta”,21);
雇员[]所有雇员={employee1,employee2,employee3,employee4,employee5,employee6};
for(int i=0;i
请参见链接重复问题的公认答案中的“静态字段”一段。在此上下文中,您是否正在使用数组列表的另一个问题无关紧要。您为每个员工将员工id初始化为“0”,并在getNextUniqueID中将其递增1。因此,它为每个employee对象打印相同的id。静态属性在类的所有实例之间共享,因此当您修改属性时,它会为所有实例修改。如果每个实例的employeeID必须是唯一的,则应将其设置为非静态的。每次调用getNextUniqueID()
后,还需要更新employeeID
。好的,我改为private String studentID=“0”
Now,intid=Integer.parseInt(studentID)
告诉我将studentID设置为静态,因为方法是公共静态字符串getNextUniqueID()
是静态的。根据我的项目,我必须让getNextUniqueID静态查看链接重复问题的公认答案中的“静态字段”一段。在此上下文中,您是否正在使用数组列表的另一个问题无关紧要。您为每个员工将员工id初始化为“0”,并在getNextUniqueID中将其递增1。因此,它为每个employee对象打印相同的id。静态属性在类的所有实例之间共享,因此当您修改属性时,它会为所有实例修改。您应该创建employeeID
n