简体   繁体   English

Java通过最终数组定义类成员

[英]Java define class members by a final array

I have an array which contains some definitions which are used in the program: 我有一个数组,其中包含一些在程序中使用的定义:

final String[] names = new String[]{"familyNames", "givenNames", "middleNames", "nickNames", "additionalNames", ..., ..., ..., ...}; // Lots of definitions

And now I need to create a new class with these definitions: 现在,我需要使用以下定义创建一个新类:

public class Person {
  final private String id;
  final private String familyNames;
  final private String givenNames;
  final private String middleNames;
  final private String nickNames;
  final private String additionalNames;
  ...
  ...
  ...
  ...
  ... // Very long list
}

Is it possible to define this class by that array? 是否可以通过该数组定义此类? Something like: 就像是:

public class Person {
  final private String id;
  final private .... names...
  public int compareTo(...) {...}
}

This Person class should support sort by multi fields, eg first by given name, then by family name, etc. Person类应支持按多个字段排序,例如,首先按给定名称,然后按姓氏等。

To avoid a long list of class member variables, you may use a map of attributes as mentioned here: 为了避免列出一长串的类成员变量,可以使用此处提到的属性映射:

Map<String,String> attributesMap = new HashMap<String,String>();

"familyNames", "givenNames", "middleNames", "nickNames", "additionalNames", etc should be used as keys. 应该使用“ familyNames”,“ givenNames”,“ middleNames”,“ nickNames”,“ additionalNames”等作为键。

You could define it in a class Person by taking the attributesMap in a static factory method. 您可以通过采用静态工厂方法中的attributesMap在Person类中定义它。

public class Person {
  final private String id;
  final private Name name;
  ...
  public static Person fromAttributesMap(Map<String, String> attributesMap) {
      Person person = new Person();
      person.id = attributesMap.get("id");
      ...
  }
}

But do keep related attributes together in their own classes. 但是,请务必将相关属性放在自己的类中。 For eg; 例如 you could club familyName , givenName , nickName , etc. in a Name class: 你能俱乐部familyNamegivenNamenickName等的Name类:

class Name {
  final private String familyName;
  final private String givenName;
  final private String middleName;
}

I'm not in full agreement to the map approach. 我对地图方法并不完全同意。 Infact, there is a code smell around the same - it is called "Primitive obsession" 实际上,周围有一种代码气味-称为“原始痴迷”

You could also sort a List<Person> , for example by the family name: 您还可以按名称对List<Person>进行排序:

public static Comparator<Person> FamilyNameComparator 
                      = new Comparator<Person>() {

    public int compare(Person person1, Person person2) {


      //ascending order
      return person1.getFamilyName().compareTo(person2.getFamilyName());
    }

};

and then calling: 然后调用:

Arrays.sort(persons, Person.FamilyNameComparator);

You could also sort by multiple fields. 您还可以按多个字段排序。 Take a look at this question on SO. 看看关于SO的这个问题。

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

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