[英]ID auto increment for extended class object java
Have a problem, my ID auto increment code is not working.有问题,我的 ID 自动递增代码不起作用。 ID for object is always 0. Maybe someone can help me with solving this.
object 的 ID 始终为 0。也许有人可以帮我解决这个问题。 Tried different ways, but still not working for me.
尝试了不同的方法,但仍然不适合我。 For my case I should use Employee class for ID counter.
对于我的情况,我应该使用 Employee class 作为 ID 计数器。 but Im not sure that Im doing correct.
但我不确定我做的是否正确。
Superclass超类
public class Person {
public String PersonName;
public String PersonSurname;
public Person()
{
this.PersonName = "";
this.PersonSurname = "";
}
public Person(String PersonName, String PersonSurname)
{
this.PersonName = PersonName;
this.PersonSurname = PersonSurname;
}
@Override
public String toString()
{
return "Person name, surname: "+ this.PersonName + " " + this.PersonSurname;
}
public void setPersonName(String PersonName)
{
this.PersonName = PersonName;
}
public void setPersonSurname(String PersonSurname)
{
this.PersonSurname = PersonSurname;
}
public String getPersonName()
{
return PersonName;
}
public String getPersonSurname()
{
return PersonSurname;
}
class with id counter class 带 id 计数器
public class Employee extends Person {
private int EmployeeID;
private static int IdCounter = 0;
public Employee(){
super("","");
EmployeeID = 0;
this.EmployeeID = IdCounter++;
}
public Employee(int EmployeeID, String PersonName, String PersonSurname){
super(PersonName,PersonSurname);
this.EmployeeID = IdCounter++;
}
@Override
public String toString()
{
return "Person name, surname: " +this.EmployeeID + ". " + this.PersonName + " " + this.PersonSurname;
}
public void setEmployeeID(int EmployeeID)
{
this.EmployeeID = EmployeeID;
}
public int getEmployeeID()
{
return EmployeeID;
}
}
my main class我的主class
public class Main {
public static void main(String[] rez) {
Scanner scanner = new Scanner(System.in);
String firstDigit = "";
String secDigit = "";
Employee CompanyEmployee = new Employee();
do {
System.out.println("Please input Name: ");
firstDigit = scanner.nextLine();
System.out.println("Please input Surname: ");
secDigit = scanner.nextLine();
CompanyEmployee.setPersonName(firstDigit);
CompanyEmployee.setPersonSurname(secDigit);
System.out.println(CompanyEmployee);
}
while (!firstDigit.equals("dasd"));
Please update your code like below请更新您的代码,如下所示
public class Main {
public static void main(String[] rez) {
Scanner scanner = new Scanner(System.in);
String firstDigit = "";
String secDigit = "";
List<Employee> listofemployee = new ArrayList<>();
do {
Employee companyEmployee = new Employee();
System.out.println("Please input Name: ");
firstDigit = scanner.nextLine();
System.out.println("Please input Surname: ");
secDigit = scanner.nextLine();
companyEmployee.setPersonName(firstDigit);
companyEmployee.setPersonSurname(secDigit);
System.out.println(companyEmployee);
listofemployee.add(companyEmployee);
}
while (!firstDigit.equals("dasd"));
I guess you don't need that EmployeeID variable.我猜你不需要那个 EmployeeID 变量。
public class Employee extends Person {
private static int id = 0; //renamed your IdCounter variable to just id
public Employee(){
super("","");
id++; //will increment every time you construct an object
}
public Employee(int EmployeeID, String PersonName, String PersonSurname){
super(PersonName,PersonSurname);
id++; //will increment every time you construct an object
}
public getId(){
return id;
}
I would advice you to use randomizer for giving an id to you objects, because if you run programm again id filed becomes 0 and new objects will get same id as previous ones我建议您使用随机化器为您的对象提供 id,因为如果您再次运行程序,id 字段将变为 0,并且新对象将获得与以前的对象相同的 id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.