我正在编写一个简单的程序来列出JAVA中的公司名称和员工队伍。
我想动态创建对象。以下是代码
public class EmployeeRecord {
/**
* @param args
*/
String company, name;
int employee;
public String input;
public static BufferedReader br;
public int iE;
public static String numberOfCompanies;
String nameOfCompany;*/
public void company(String input) {
// TODO Auto-generated method stub
nameOfCompany = input;
}
public void employee(String employeeNumber) {
// TODO Auto-generated method stub
iE = Integer.parseInt(employeeNumber);
}*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EmployeeRecord qw = new EmployeeRecord ();
try {
br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number of companies: ");
numberOfCompanies = br.readLine();
int G = Integer.parseInt(numberOfCompanies);
for (int i = 1; i <= G; i++) {
System.out.println("Enter name of the company: ");
String company = br.readLine();
qw.company(company);
System.out.println("Enter Number of employees: ");
String employeeNumber = br.readLine();
qw.employee(employeeNumber);
}
for (int i = 1; i <= G; i++) {
qw.sortCompanySummary();
}
} catch (IOException io) {
io.printStackTrace();
}
}
public void companySummary() {
System.out.println("Number of companies: " + numberOfCompanies);
System.out.println("Name of company: "+nameOfCompany);
System.out.println("Number of employees: "+iE);
}
}
我想在这里做的是动态创建EmployeeRecord类的单独实例。例如
EmployeeRecord qw = new EmployeeRecord();
EmployeeRecord we = new EmployeeRecord();
答案 0 :(得分:2)
首先,您需要将目标代码与控制代码分开。
其次,您需要某种集合或数组来保存您的对象。
以下是您的代码外观的概念:
public class UI{ // <---- this class will control the flow of your program
public static void main(String[] args){
private List<Company> company; // <---- this Collection object holds many Company objects
...
for(int i=0;i<company.size();i++){
Company c = new Company();
c.setName(br.readLine());
List<Employee> employee = new ArrayList<Employee>();
...
for(int j=0;j<employee.size();j++){
Employee e = new Employee();
e.setName(br.readLine());
...
employee.add(e);
}
c.setEmployee(employee);
company.add(c);
}
}
}
public class Company{ // <---- this class will represent the companies
private List<Employee> employee;
private String name;
public void setEmployee(List<Employee> employee){
this.employee = employee;
}
....
}
public class Employee{ // <----- this class will represent the employees
private String name;
private int empNo;
public int getEmpNo(){
return empNo;
}
...
}
答案 1 :(得分:2)
我没有正确理解这个问题,但仔细查看代码,我相信你需要在循环中创建对象,因为你正在从用户那里获取输入。这是您需要做的:
ArrayList<EmployeeRecord> qwList = new ArrayList<EmployeeRecord>();
在要求用户输入之前声明列表。 现在在循环内创建对象,为它们分配值并将这些对象添加到列表中。这是您可以在列表中执行的操作
for (int i = 1; i <= G; i++) {
EmployeeRecord qw = new EmployeeRecord ();
System.out.println("Enter name of the company: ");
String company = br.readLine();
qw.company(company);
System.out.println("Enter Number of employees: ");
String employeeNumber = br.readLine();
qw.employee(employeeNumber);
qwList.add(qw);
}
对于每个公司,已在列表中插入新对象。现在,您可以使用此列表执行任何操作。打印所有记录或对其进行排序。
答案 2 :(得分:1)
根据您发布的代码以及您提到的对象的动态创建,我认为唯一的方法是您应该查看Collections Framework。