Java 如何编写一个id为字符串的静态类

Java 如何编写一个id为字符串的静态类,java,Java,我想编写一个以C1000开头的静态类customer ID,对于创建的每个新customer对象,它将添加+1、C1001、C1002、C1003等等。如果有一个字符串,它是如何完成的 public class Customer { private static int customerID = 1000; public Customer() { customerID++; } public static int getcutomerI

我想编写一个以C1000开头的静态类customer ID,对于创建的每个新customer对象,它将添加+1、C1001、C1002、C1003等等。如果有一个字符串,它是如何完成的

public class Customer
{
    private static int customerID = 1000;

    public Customer()
    {
        customerID++;
    }

    public static int getcutomerID()
    {
        return customerID;
    }
}
不要那样做。将int与一起使用。

不要这样做。将int与

静态对测试非常不利。它相当于全局变量。也许更好的设计是:

public class Customer {
    private final int id;
    public Customer(final int id) { this.id = id; }
    public int getId() { return id; }
}

public class CustomerDatabase {
    private int nextId;

    public CustomerDatabase() { this(0); }
    public CustomerDatabase(int nextId) { this.nextId = nextId; }

    public synchronized int nextId() { return nextId++; }

    // define useful operations for a CustomerDatabase
}

// maybe you could use the database and customer classes as follows
public class CustomerApplication {
    public static void main(String[] args) {
        // first argument is highest customer id
        CustomerDatabase db = new CustomerDatabase(Integer.parseInt(args[0]));

        Customer c = new Customer(db.nextId());
        db.add(c);

        System.out.println(db.getCustomers());

        db.save("customers.txt");

        Customer x = db.findById(13);
        if(x.isBroke()) db.delete(x);

        System.out.println("K thx, bai");
    }
}
静态对测试非常不利。它相当于全局变量。也许更好的设计是:

public class Customer {
    private final int id;
    public Customer(final int id) { this.id = id; }
    public int getId() { return id; }
}

public class CustomerDatabase {
    private int nextId;

    public CustomerDatabase() { this(0); }
    public CustomerDatabase(int nextId) { this.nextId = nextId; }

    public synchronized int nextId() { return nextId++; }

    // define useful operations for a CustomerDatabase
}

// maybe you could use the database and customer classes as follows
public class CustomerApplication {
    public static void main(String[] args) {
        // first argument is highest customer id
        CustomerDatabase db = new CustomerDatabase(Integer.parseInt(args[0]));

        Customer c = new Customer(db.nextId());
        db.add(c);

        System.out.println(db.getCustomers());

        db.save("customers.txt");

        Customer x = db.findById(13);
        if(x.isBroke()) db.delete(x);

        System.out.println("K thx, bai");
    }
}
请注意,这可能不是线程安全的。如果需要,请查看并使用其中一个用于NextCustomerId


请注意,这可能不是线程安全的。如果需要,请查看并使用其中一个用于NextCustomerId。

不。。。他最好不要使用静态变量。。。他应该将发布新客户ID的问题从客户类中分离出来——可能创建一个IdGenerator或CustomerDatabase,正如我在修改后的响应中所包含的那样。如果没有静态,则易于测试。如果有静态,序列化是如何工作的?一些问题突然出现在。。。静态…不。。。他最好不要使用静态变量。。。他应该将发布新客户ID的问题从客户类中分离出来——可能创建一个IdGenerator或CustomerDatabase,正如我在修改后的响应中所包含的那样。如果没有静态,则易于测试。如果有静态,序列化是如何工作的?一些问题突然出现在。。。静态…这似乎是一种过激行为,没有更好的方法来解决他的问题吗?这似乎是一种过激行为,没有更好的方法来解决他的问题吗?
public class Customer {
    private static int NextCustomerId = 1000;
    private final int myCustomerId;

    public Customer() {
        myCustomerId = NextCustomerId++;
        ...
    }

    public String getCustomerId() {
        return "C" + myCustomerId;
    }
}