我想要一个变量,该变量可以计算main中所有已创建对象中所有雇员的数量。这是我走了多远:

public class test {

    public static void main(String[] args) {
        Department dep1 = new Department();
        Department dep2 = new Department("CSTI", 30);
        dep1.setEmp(20);
        dep2.setEmp(40);
        System.out.println(dep1.totalNumEmp);
        // Outputs 60
        dep1 = dep2; // here is where I get lost
        dep1.setEmp(10);
        System.out.println(dep1.numEmp + " " + dep2.numEmp);// outputs 10 10
        System.out.println(dep1.totalNumEmp);// Outputs 30. When it needs to output 20, because dep1 and dep2 are now
                                                // both 10
    }
}

class Department {

    String name;
    int numEmp;

    public static int totalNumEmp;

    Department() {
    }

    Department(String newName, int newNum) {
        name = newName;
        numEmp = newNum;
        totalNumEmp += numEmp;
    }

    public void setEmp(int newNum) {
        totalNumEmp -= numEmp;
        numEmp = newNum;
        totalNumEmp += numEmp;
    }
}


我的问题是我不知道应该如何在dep1 = dep2之后正确更新totalNumEmp。在将totalNumEmp指向dep1的引用后,我必须想出一种方法来使dep2正确更新。我知道我可以轻松地执行dep1.setEmp(dep2.numEmp)并避免将其弄乱totalNumEmp,但是我必须知道如何使用dep1指向dep2来做到这一点。

最佳答案

免责声明:我会尽量简化。无需数据封装,模式,并发等。在学习基础知识时,请使其尽可能简单。

首先,您需要了解自己在做什么。


  我们的老师希望我们有一个可以计算所有数字的变量
  员工...


在这里,我看到一个带有Department方法的setEmp类,它似乎在每次调用时增加了雇员人数。我不认为这是你老师的意思


  ...遍及main中所有已创建的对象。


您的老师最有可能希望您做的是开设两节课(我假设您创建Department是有原因的):

Employee


分配给

Department




再次


  ...统计了所有已创建员工的总数
  主要对象


我们可以用两种方式解释


您的老师希望您对所有创建的Employee进行计数(问题标题表明)
您的老师希望您计算每个Employee的所有Department


让我们从选项1开始

我将跳过Department类,因为它实际上不在范围内,我将直接转到创建的员工数。
分配的任务将只计算创建的对象,这意味着创建的Employee

这是Employee类的外观

public class Employee {
   // Count of the created employees
   public static int count = 0;

   // ... Class fields

   public Employee(
       final Department department,
       final String name,
       final String surname) {
      // ... Assign arguments to class fields

      // A new employee has been created. Increment the counter!
      ++count;
   }

   ...
}


这个静态场

public static int count = 0;


每次创建新的Employee时都会递增。
您将可以通过访问它

final int total = Employee.count;




现在,选项2

由于需要为每个Employee计算Department个,因此需要在Department类中增加一种计数。这就是Department类的样子

public class Department {
   private final List<Employee> employees;
   private final String name;

   public Department(final String name) {
      this.employees = new ArrayList<Employee>();
      this.name = name;
   }

   public String getName() {
      return name;
   }

   public List<Employee> getEmployees() {
      return employees;
   }

   public int getEmployeeCount() {
      return employees.size();
   }

   public void addEmployee(final Employee employee) {
      employees.add(employee);
   }
}


您可以看到该类拥有


一个名字
分配的员工列表(private final List<Employee> employees


然后,我们有了Employee类,就像我上面描述的那样,但是要注意更改

public class Employee {
   // ... Class fields

   public Employee(
       final String name,
       final String surname) {
      // ... Assign arguments to class fields
   }

   ...
}


在这里,我们只有一个普通班(Pojo)。我们如何使用它?遵循代码内的注释

public static void main(final String[] args) {
   final Department dep1 = new Department("dep1");
   dep1.addEmployee(new Employee("Name1", "Surname1"));
   dep1.addEmployee(new Employee("Name2", "Surname2"));

   final Department dep2 = new Department("dep2");
   dep2.addEmployee(new Employee("Name3", "Surname3"));

   // Now we have two departments.
   // We can retrieve the count of employees for each using
   final int dep1Count = dep1.getEmployeeCount();
   final int dep2Count = dep2.getEmployeeCount();

   // And we can have the total
   final int total = dep1Count + dep2Count;
}




我没有包含有关同步或并发的对象或主题,因为我认为您仍处于起步阶段,不需要与这些东西混淆。

关于java - 保持静态变量在整个对象中正确更新的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55202221/

10-13 07:32