我有Java的基础知识,想要为我的员工创建独特的ID,但我不想使用java.util.UUID。我该怎么做?在哪里以及应该向我的代码添加什么方法?谢谢。
import java.util.ArrayList;
public class Main {
private static ArrayList<Employees> list = new ArrayList<>();
public static void main(String[] args) {
Employees emp =new Employees(15, "xx", 23);
Main.add(emp);
}
public static void delete(int id) {
list.remove(get(id));
}
public static void updateName(int id, String name) {
get(id).name=name;
}
public static void updateAge(int id, int age) {
get(id).age=age;
}
public static Employees get(int id) {
for(Employees emp : list)
if(emp.id==id)
return emp;
throw new RuntimeException("Employees with id : "+id+" not found");
}
}
class Employees {
String name;
int age;
int id ;
public Employees(int id, String name, int age) {
//super();
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return id+" : "+" "+name+", "+age+" ans";
}
}
Main.add()
你确定吗?^^ "EmployeeS" 带 s 吗?一个简单的方式是在 Employees 类中使用静态计数器作为 ID 并递增它。 - azro