如何从类中获取对象?

时间:2021-08-15 19:41:53

I have a class Person with 2 fields: Name and Age and I want to declare instance of this class inside the class like this.

我有一个包含2个字段的Person:Name和Age,我想在类中声明这个类的实例。

public class Person implements ToCSV {

private String name;
private int age;

public Person(String name, int age) {
    this.name = name;
    this.age = age;
}

public Person(){

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

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


Person p1 = new Person("Patrick", 24);
Person p2 = new Person("Bernard", 20);
Person p3 = new Person("Lam", 23);

}

My question is: Can I take all the value of object p1, p2, p3 by the general way?

我的问题是:我可以通过一般方式获取对象p1,p2,p3的所有值吗?

My goal is save all object's data to a file like txt... The file will have something like this: "Patrick - 24, Bernard - 20, Lam - 23" and I want something so general for automatically generate the file txt. So I have to try to retrieve the object of class

我的目标是将所有对象的数据保存到文件,如txt ...文件将具有如下内容:“Patrick - 24,Bernard - 20,Lam - 23”,我想要一些如此通用的自动生成文件txt。所以我必须尝试检索类的对象

I tried to use Java Reflection. It recognized p1, p2, p3 like the Fields but I cannot access to the value of this object. Any idea? Thanks for your attention!

我尝试使用Java Reflection。它像Fields一样识别p1,p2,p3,但我无法访问该对象的值。任何的想法?感谢您的关注!

1 个解决方案

#1


1  

Interestingly, the code you posted will produce a *Error - each constructor call to the first constructor will try to produce 3 more Person instances. Did you mean to make the Person fields static?

有趣的是,您发布的代码将产生*Error - 对第一个构造函数的每个构造函数调用将尝试生成另外3个Person实例。你的意思是将Person字段设为静态吗?

If you did, you could do this:

如果你这样做,你可以这样做:

for (Field f : Person.class.getDeclaredFields()) {
    try {   
        System.out.println(((Person)f.get(null)).getName());
    } catch (IllegalAccessException e) {
        // do something
    }
}
Patrick
Bernard
Lam

I generally try to persuade people to look for a method other than reflection if it's at all possible. In your case, you might be able to store p1..p3 in an array and then loop through the array.

如果可能的话,我通常会试图说服人们寻找除反思以外的方法。在您的情况下,您可能能够将p1..p3存储在数组中,然后循环遍历该数组。

#1


1  

Interestingly, the code you posted will produce a *Error - each constructor call to the first constructor will try to produce 3 more Person instances. Did you mean to make the Person fields static?

有趣的是,您发布的代码将产生*Error - 对第一个构造函数的每个构造函数调用将尝试生成另外3个Person实例。你的意思是将Person字段设为静态吗?

If you did, you could do this:

如果你这样做,你可以这样做:

for (Field f : Person.class.getDeclaredFields()) {
    try {   
        System.out.println(((Person)f.get(null)).getName());
    } catch (IllegalAccessException e) {
        // do something
    }
}
Patrick
Bernard
Lam

I generally try to persuade people to look for a method other than reflection if it's at all possible. In your case, you might be able to store p1..p3 in an array and then loop through the array.

如果可能的话,我通常会试图说服人们寻找除反思以外的方法。在您的情况下,您可能能够将p1..p3存储在数组中,然后循环遍历该数组。