Java自动增量id[英] Java auto increment id

问题描述

我正在用 Java 进行一些编码,但它不起作用:

public class job
{
   private static int count = 0; 
   private int jobID;
   private String name;
   private boolean isFilled;

   public Job(, String title, ){
      name = title;
      isFilled = false;
      jobID = ++count; 
  }
}

我需要在创建新条目时自动增加 ID.

推荐答案

试试这个:

public class Job {
  private static final AtomicInteger count = new AtomicInteger(0); 
  private final int jobID;
  private final String name;

  private boolean isFilled;

  public Job(String title){
    name = title;
    isFilled = false;
    jobID = count.incrementAndGet(); 
}

本文地址:https://www.itbaoku.cn/post/978684.html