简体   繁体   English

动态命名对象

[英]Dynamic naming of Objects

I am doing a project for a class and I am required to create a program where you can input "Employee data" such as name, employee number, department, and position into the program and then have the program read that information back out to you. 我正在为一个班级做一个项目,我需要创建一个程序,您可以在其中输入“员工数据”,例如姓名,员工编号,部门和职位,然后让程序将这些信息读回给您。

All of this is to be done through the console to provide input for the names. 所有这些都将通过控制台来完成,以提供名称的输入。

Catch is I don't know how many employees they are going to throw at my program. 抓住的是,我不知道他们要在我的计划中投入多少员工。 I need a way to dynamically create objects from my "Employee" class and name them. 我需要一种从“ Employee”类动态创建对象并为其命名的方法。

Does anyone have any suggestions. 有没有人有什么建议。 Unfortunately I don't have any code to write because I am at a loss of where to begin. 不幸的是,我没有任何代码可写,因为我不知道从哪里开始。 I have created my employee class with methods that can set each of the parameters, but again, the issue is creating the class from the console in the first place. 我用可以设置每个参数的方法创建了我的员工类,但是同样,问题是首先要从控制台创建该类。

You don't need to name the objects dynamically (actually, you probabely don't need a reference to the object at all). 您不需要动态命名对象(实际上,您根本不需要引用该对象)。 Just store the created objects in a collection. 只需将创建的对象存储在集合中。

You can use a ArrayList, check the implementation: 您可以使用ArrayList,检查实现:

Declare and initalize variable: 声明并初始化变量:

ArrayList<Employee> empArray = new ArrayList<Employee>();

Add employee to the array: 将员工添加到数组:

empArray.add(aEmployee);

Retrive employee from array: 从数组中检索雇员:

empArray.get(0);

Here you have more documentation: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html 这里您有更多文档: https : //docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

  • Create an employee object with feilds like name, department, position etc. 创建具有名称,部门,职位等领域的员工对象。
  • Then create a HashSet or ArrayList of these objects 然后创建这些对象的HashSet或ArrayList
  • Then write code that reads in the input 然后编写读取输入的代码
  • After each complete employee is read, create an employee object with the input 读取每个完整的员工后,使用输入创建一个employee对象
  • Add the employee object to the List or Set. 将雇员对象添加到列表或集合。

When all the employee data is read in, loop through your List or Set and print out the employee objects. 读完所有员工数据后,遍历您的“列表”或“设置”并打印出员工对象。

edit: Responding to your comment here, since comments cannot be formatted: 编辑:在这里回应您的评论,因为评论无法格式化:

Can you set up a ListArray of Strings? 您可以设置字符串列表数组吗? Its like this: 就像这样:

List<String> myList=new ArrayList<String>();

Just like that, you make a ListArray of your employee object: 就像这样,您将创建一个Employee对象的ListArray:

List<Employee> employeeList=new ArrayList<Employee>();

You create the List once, and you create Employee objects for each employee. 您一次创建列表,然后为每个员工创建Employee对象。 You create a new Employee object with new Employee(). 您使用new Employee()创建一个新的Employee对象。 Assuming you have written the code for the Employee object with getters and setters, its something like this: 假设您已经使用getter和setter编写了Employee对象的代码,如下所示:

Employee employee;

// ... in a loop, reading all the input data..
employee = new Employee();
employee.setName("Reggie");
employeeList.add(employee);
//... end your loop here....

Once you have created and added all the employees, you just loop through the list with a for loop: 一旦创建并添加了所有雇员,就可以使用for循环遍历列表:

for(Employee emp:employeeList) {
    System.out.println(emp.getName());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM