简体   繁体   English

如何在不明确键入每个项的属性的情况下将Java getter和setter与一组数据一起使用?

[英]How do I use Java getters and setters with a collection of data without explicitly typing out the attributes for each item?

I am very new to Java and to programming in general, and I have an assessment to complete where I load employees (with name, age, and department attributes; department can be only one of four enumerated values) into a program that will sort them by age and tell if the age is a prime number. 我对Java和一般的编程都很陌生,我有一个评估来完成将员工(名称,年龄和部门属性;部门只能是四个枚举值中的一个)加载到一个程序中的位置,它将对它们进行排序按年龄分辨年龄是否为素数。 The assignment requires Company, Department, and Employee classes. 该作业需要公司,部门和员工类。 I am confident that I can figure out age/prime components — I know how to google for algorithms. 我相信我可以找出年龄/主要成分 - 我知道如何谷歌算法。 What I am struggling with is putting all the discrete pieces into a cohesive whole. 我正在努力的是将所有不连续的部分组合成一个有凝聚力的整体。

Here is what I have so far. 这是我到目前为止所拥有的。 I've put in one employee, but the way I'm doing it seems completely inelegant and inefficient. 我已经安排了一名员工,但我这样做的方式似乎完全不优雅且效率低下。 I am sure there is a better way, but I've hit a mental block. 我相信有更好的方法,但我遇到了一个心理障碍。

EDIT: as was pointed out below, I was unclear. 编辑:如下所述,我不清楚。 What I am asking help with is populating the data structure. 我要求帮助的是填充数据结构。

Company class: 公司课程:

public class Company {

static Employee one = new Employee();



public static void main(String[] args) {

    one.setName("Counting Guru");
    one.setAge(55);
    one.setDepartment(DepartmentList.ACCOUNTING);


}

}

DepartmentList class: DepartmentList类:

import java.util.EnumMap;
import java.util.Map;
import java.util.Set;
public enum DepartmentList {
ACCOUNTING, MARKETING, HUMANRESOURCES, INFORMATIONSYSTEMS;




public static void main(String[] args) {
Map<DepartmentList,String> 
enumMap=new EnumMap<DepartmentList,String>(DepartmentList.class);
enumMap.put(DepartmentList.ACCOUNTING, "Accounting");
enumMap.put(DepartmentList.MARKETING, "Marketing");
enumMap.put(DepartmentList.HUMANRESOURCES, "Human Resources");
enumMap.put(DepartmentList.INFORMATIONSYSTEMS, "Information Systems");

 Set<DepartmentList> keySet = enumMap.keySet();
 for (DepartmentList department : keySet) {
     String value = enumMap.get(department);
     System.out.println("ENUMMAP VALUE:"+value);
 }
}

}

Employee class: 员工类:

public class Employee {

String empName;
int empAge;
DepartmentList empDept;


Employee() {

}

public String getName() {
    return empName;
}



public void setName(String name) {
    this.empName = name;
}



public int getAge() {
    return empAge;
}



public void setAge(int age) {
    this.empAge = age;
}



public DepartmentList getDepartment() {
    return empDept;
}



public void setDepartment(DepartmentList department) {
    this.empDept = department;
}

public Employee(String empName, int empAge, DepartmentList empDept){

}


}

I also have a Department class, but it's currently empty. 我也有一个Department类,但它目前是空的。

Am I on the right track? 我是在正确的轨道上吗? Can someone give me a nudge? 有人能给我一个轻推吗? Thank you! 谢谢!

If you're asking if there is something like a property in Java, no, there isn't (at least not yet). 如果你在询问Java中是否存在类似属性的东西,那么就没有(至少还没有)。 If you're asking how to populate your objects something like an IOC container, like Spring, would be a better choice. 如果你问如何填充像IOC容器这样的对象,比如Spring,那将是一个更好的选择。

Now as it comes to your code you have two main methods in two different classes. 现在,对于您的代码,您在两个不同的类中有两个主要方法。 Only one will be called. 只会调用一个。 If you want to create a static instance you will be better do 如果你想创建一个静态实例,你会更好

static Employee one = new Employee("Counting Guru", 55, DepartmentList.ACCOUNTING);

or 要么

static Employee one = new Employee();
static {
    one.setName("Counting Guru");
    one.setAge(55);
    one.setDepartment(DepartmentList.ACCOUNTING);
}

When it comes to the enum then you'll better define a constructor for it 当谈到枚举时,你最好为它定义一个构造函数

public enum DepartmentList {
    ACCOUNTING("Accounting"), MARKETING("Marketing");

    private String displayName;

    public DepartmentList(String displayName) {
        this.displayName = displayName;
    }

    public String getDisplayName() {
        return diplayName;
    }
 }

In the Employee constructor you need to assign the field values to the ones received as arguments. 在Employee构造函数中,您需要将字段值分配给作为参数接收的字段值。

Don't hard-code the data inside the Java program. 不要对Java程序中的数据进行硬编码。 Put the data in a file and write methods to load the data. 将数据放在一个文件中并编写方法来加载数据。

If you MUST hardcode the data in the program, use something like this sample: 如果您必须对程序中的数据进行硬编码,请使用以下示例:

public class Employee
{
    String name;
    int age;
    public Employee(String name, int age)
    {
        this.name = name;
        this.age  = age;
    }
    // getters, setters, etc.
}

In the main program 在主程序中

private static Employee[] empData = 
{
    new Employee("John Smith", 50),
    new Employee("Fred Jones", 25),
    .
    .
    .
};

Now you have a static array of Employee objects that you can "load" into your data structure. 现在,您有一个可以“加载”到数据结构中的静态Employee对象数组。

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

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