我想编写一个以C1000开头的静态类客户ID,对于每个创建的新客户对象,它将添加+1,C1001,C1002,C1003等.如果有字符串怎么办?
public class Customer
{
private static int customerID = 1000;
public Customer()
{
customerID++;
}
public static int getcutomerID()
{
return customerID;
}
}
Run Code Online (Sandbox Code Playgroud)
public class Customer {
private static int NextCustomerId = 1000;
private final int myCustomerId;
public Customer() {
myCustomerId = NextCustomerId++;
...
}
public String getCustomerId() {
return "C" + myCustomerId;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这可能不是线程安全的.如果你需要它,请查看java.util.concurrent.atomic.AtomicInteger并使用其中一个NextCustomerId.