Java 整数属性作为自动生成的数字

Java 整数属性作为自动生成的数字,java,javafx,Java,Javafx,我正在创建一个简单的Java应用程序,它将为客户存储和显示信息 我想将id作为一个自动生成的数字,但有问题,不知道应该在get或set方法中设置它吗? 有谁能帮我把这个值用作 以下是一个例子: public class Customer{ public Person(String firstName, String lastName, String email, String address, String country){ this.id.set(Integer.parseInt(

我正在创建一个简单的Java应用程序,它将为客户存储和显示信息

我想将
id
作为一个自动生成的数字,但有问题,不知道应该在get或set方法中设置它吗? 有谁能帮我把这个值用作

以下是一个例子:

public class Customer{

public Person(String firstName, String lastName, String email, String address, String country){
    this.id.set(Integer.parseInt(UUID.randomUUID().toString()));
    this.firstName.set(firstName);
    this.lastName.set(lastName);
    this.email.set(email);
    this.address.set(address);
    this.country.set(country);
}

private final IntegerProperty id = new SimpleIntegerProperty(this,"Id",0);
private final StringProperty firstName = new SimpleStringProperty(this,"First Name","");
private final StringProperty lastName = new SimpleStringProperty(this,"Last Name","");
private final StringProperty email = new SimpleStringProperty(this,"E-mail","");
private final StringProperty address = new SimpleStringProperty(this,"Address","");  
private final StringProperty country = new SimpleStringProperty(this,"Country","");
我还创建了泛型bean方法,但很简单,如下所示:

public StringProperty firstNamePropery(){
    return firstName;
}
public String getFirstName(){
    return firstName.get();
}
public void setFirstName(String firstName){
    this.firstName.set(firstName);
}
//…其余的方法。。。 我尝试使用此选项,但不起作用:

public IntegerProperty idProperty(){
    return id;
}
public Integer getId(){
    return id.get();
}
public void setId(){
    this.id.set(Integer.parseInt(UUID.randomUUID().toString()));
}

谢谢您在这一点上的帮助。

UUID字符串看起来像
38400000-8cf0-11bd-b23e-10b96e4ef00d
。无法将此字符串解析为整数

如果您想使用UUID作为客户的ID,那么将属性声明为UUID或字符串,而不是整数

编辑I

另外,我不需要将其存储为整数值,字符串可以 作业,但在创建新作业时无法创建该编号 该类的实例

要将
UUID
用作字符串,请执行以下操作: 在
Customer
类中,id属性的类型必须是
String
,而不是
Integer
(或
int
)。
要获取
UUID
的新
字符串
表示,可以调用
UUID.randomUUID().toString()
。此调用的结果可以分配给客户的
id
,而无需进行任何解析

还要注意,
getter
setter
的签名必须相应地更改

在当前的
setId()
方法中,您正在创建一个新的
id
。这将覆盖在使用构造函数中的调用创建客户时分配的
id
。如果希望灵活地分配新id,可以让
setId
接收新的
UUID
字符串,并将其作为新的
id
分配给
Customer
对象

public class Customer{

    public Customer(String firstName, String lastName, String email, String address, String country){
       this.id.set(UUID.randomUUID().toString());
    }

    ...

    public String getId(){
       return this.id;
    }

    public void setId(String newId){
       this.id = newId;
    }
}
注意:类名是Customer,构造函数是Person。这是错误的,两者必须具有相同的名称。告诉您这一点,您一定有编译器错误。我将假定类和构造函数的正确名称为Customer

/EDIT I

UUID的用例是当您需要一个唯一的id而不检查该id是否已经存在于其他方(例如数据库引擎或网络应用程序中没有中央服务器的服务器)时

如果您想要使用的是整数(或长),那么没有真正的理由使用随机数,您可以使用序列号作为ID

如果在javafx中是一个独立的应用程序,并且您没有使用不同的线程来并行创建客户,那么就没有太多需要担心的了

另一方面,如果它是客户机-服务器应用程序。然后您必须考虑客户端对服务器的并发访问

如果将id创建作为数据库中的一个序列进行委托,则并发问题或在id中生成副本将由数据库本身负责。这可以是同一客户表中的自动增量字段(假设您正在使用)、序列或充当序列的表。另一方面,如果您的类将一个接一个地生成ID,那么您必须处理并发请求。您必须确保一次只有一个线程可以增加id


关于getter和setter,
getXxx()
返回
xxx
属性的值。而
setXxx(123)
会将值
123
设置或分配给属性
xxx

UUID字符串类似于38400000-8cf0-11bd-b23e-10b96e4ef00d。无法将此字符串解析为整数。即使在JVM重新启动后,ID是否仍必须保持唯一?跨多个并发运行的JVM如何?数据将存储在哪里?在这一点上,我将Preferences类与JAXB结合使用,因为它是一个非常简单的应用程序,并不复杂。我尝试使用Math.random()代替UUID,但仍然没有进展……而且,我不需要将其存储为整数值,字符串可以完成任务,但在创建该类的新实例时无法创建该数字。