![](/img/trans.png)
[英]create Unique id in a private method createaccountID() without using the uuid
[英]create unique id without using constructure and setters
class person {
private int id ;
private String name;
private boolean gender;
public person() {
}
public AtomicLong getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
}
我想在不使用構造函數和 setter 的情況下在這個類中創建唯一的 id。
要構造一個person
實例,字段初始值設定項將被復制到構造函數中。 假設沒問題,你可以使用AtomicInteger
東西,
private static AtomicInteger ai = new AtomicInteger(0);
private int id = ai.incrementAndGet();
你可以添加:
private static int ID_GENERATOR = 0;
然后,在構造函數中,您將使用:
public person() {
id = ID_GENERATOR++;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.