如何迭代类成员?
2022-08-31 23:53:30
我正在使用Java版本的Google App Engine。
我想创建一个可以接收许多类型的对象作为参数的函数。我想打印出对象的成员变量。每个对象可能不同,该函数必须适用于所有对象。我必须使用反射吗?如果是这样,我需要编写什么样的代码?
public class dataOrganization {
private String name;
private String contact;
private PostalAddress address;
public dataOrganization(){}
}
public int getObject(Object obj){
// This function prints out the name of every
// member of the object, the type and the value
// In this example, it would print out "name - String - null",
// "contact - String - null" and "address - PostalAddress - null"
}
如何编写函数 getObject?