Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将对象属性名与字符串进行比较?_Java_Compare_Object Property - Fatal编程技术网

Java 如何将对象属性名与字符串进行比较?

Java 如何将对象属性名与字符串进行比较?,java,compare,object-property,Java,Compare,Object Property,是否有方法比较对象中的属性是否等于字符串 下面是一个名为Person public class Person { private String firstName; private String lastName; public Person(String firstName, String lastName){ super(); this.firstName = firstName; this.lastName = l

是否有方法比较
对象中的属性是否等于字符串

下面是一个名为
Person

public class Person {

    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName){
        super();
        this.firstName = firstName;
        this.lastName = lastName;
    }

    //.... Getter and Setter

}
现在我有了一个方法,我需要检查该字符串是否与
Person
属性名相同

public boolean compareStringToPropertName(List<String> strs, String strToCompare){
    List<Person> persons = new ArrayList<Person>();
    String str = "firstName";

    // Now if the Person has a property equal to value of str, 
    // I will store that value to Person.
    for(String str : strs){

        //Parse the str to get the firstName and lastName
        String[] strA = str.split(delimeter); //This only an example

        if( the condintion if person has a property named strToCompare){
            persons.add(new Person(strA[0], strA[1]));
        }
    }

}
public boolean compareStringToPropertName(列表str、字符串strToCompare){
List persons=new ArrayList();
String str=“firstName”;
//现在如果这个人的财产价值等于str,
//我将把这个值存储给个人。
用于(字符串str:strs){
//解析str以获取firstName和lastName
String[]strA=str.split(delimeter);//这只是一个示例
if(条件if person具有名为strToCompare的属性){
人员。添加(新人员(strA[0],strA[1]);
}
}
}
我的实际问题远非如此,现在我如何知道是否需要将字符串存储到
对象的属性中。到目前为止,我的关键是我有另一个与对象属性相同的字符串

我不想有一个硬代码,这就是为什么我要达到这样的条件


总之,有没有办法知道这个字符串
(“firstName”)
的属性名与Object
(Person)

的属性名相等?您将使用反射:

更准确地说,假设您知道对象(Person)的类,您将使用class.getField(propertyName)的组合来获取表示属性的Field对象,以及Field.get(Person)的组合来获取实际值(如果存在)。如果该属性不是空的,则认为该对象在此属性中具有值


如果您的对象遵循一些约定,您可以使用“JavaBeans”特定的库,例如:

您将使用反射:

更准确地说,假设您知道对象(Person)的类,您将使用class.getField(propertyName)的组合来获取表示属性的Field对象,以及Field.get(Person)的组合来获取实际值(如果存在)。如果该属性不是空的,则认为该对象在此属性中具有值


如果您的对象遵循某些约定,您可以使用“JavaBeans”特定的库,例如:

您可以使用获取所有声明的字段,然后将其与字符串进行比较


例如:

class Person {
    private String firstName;
    private String lastName;
    private int age;
    //accessor methods
}

Class clazz = Class.forName("com.jigar.stackoverflow.test.Person");
for (Field f : clazz.getDeclaredFields()) {
      System.out.println(f.getName());
}

输出

名字
姓氏
年龄


或者

你也可以


另请参见


您可以使用获取所有声明的字段,然后将其与字符串进行比较


例如:

class Person {
    private String firstName;
    private String lastName;
    private int age;
    //accessor methods
}

Class clazz = Class.forName("com.jigar.stackoverflow.test.Person");
for (Field f : clazz.getDeclaredFields()) {
      System.out.println(f.getName());
}

输出

名字
姓氏
年龄


或者

你也可以


另请参见