【问题标题】:ID auto increment for extended class object java扩展类对象java的ID自动递增
【发布时间】:2020-10-17 20:47:10
【问题描述】:

有一个问题,我的 ID 自动递增代码不起作用。对象的 ID 始终为 0。 也许有人可以帮我解决这个问题。尝试了不同的方法,但仍然不适合我。 对于我的情况,我应该使用 Employee 类作为 ID 计数器。但我不确定我做的是否正确。

超类

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;
    }

带有 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;
    }

}

我的主要课程

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"));

【问题讨论】:

  • 您只创建一个Employee。该员工的 ID 为零。如果您想要多个具有不同 ID 的员工,请创建多个员工(例如在循环中)。
  • 嘿,谢尔盖,你认为你正在增加员工构造函数中的employeeId,所以它只是在你可以构造函数时更新值,但在你的代码中你没有在while循环中调用构造函数你只是设置值对同一个对象使用 setter 和 getter,这样员工 ID 就不会改变

标签: java class extends


【解决方案1】:

请更新您的代码,如下所示

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"));

【讨论】:

    【解决方案2】:

    我猜你不需要那个 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;
       }
    
    
    

    我建议你使用随机化器为你的对象提供一个 id,因为如果你再次运行程序,id 字段变为 0,新对象将获得与以前的对象相同的 id


    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 2011-09-16
      • 2012-01-09
      • 2017-08-01
      • 1970-01-01
      • 2013-04-11
      • 2010-10-04
      相关资源
      最近更新 更多