如何在不显式键入每个项目的属性的情况下将 Java getter 和 setter 与数据集合一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19554304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:18:14  来源:igfitidea点击:

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

javasettergetter

提问by thatpaintingelephant

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. 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.

我对 Java 和一般的编程非常陌生,我有一个评估要完成我将员工(带有姓名、年龄和部门属性;部门只能是四个枚举值之一)加载到一个程序中,对他们进行排序按年龄并判断年龄是否为质数。该作业需要 Company、Department 和 Employee 类。我有信心我可以找出年龄/主要成分——我知道如何在谷歌上搜索算法。我正在努力将所有离散的部分整合成一个有凝聚力的整体。

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:

部门清单类:

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!

我在正确的轨道上吗?有人可以给我推一下吗?谢谢!

采纳答案by Jim Garrison

Don't hard-code the data inside the Java program. Put the data in a file and write methods to load the data.

不要对 Java 程序中的数据进行硬编码。将数据放入文件并编写方法来加载数据。

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 对象的静态数组,您可以将其“加载”到您的数据结构中。

回答by Adrian Ber

If you're asking if there is something like a property in Java, no, there isn't (at least not yet). If you're asking how to populate your objects something like an IOC container, like Spring, would be a better choice.

如果你问 Java 中是否有类似属性的东西,不,没有(至少现在没有)。如果你问如何填充你的对象,比如 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 构造函数中,您需要将字段值分配给作为参数接收的字段值。