我有POJO课程
Class Book {
private String id;
private String title;
Public Book() {
}
//implement setter and getter
..............
}
main() {
Book book = new Book();
book.setId(1);
book.setTitle("new moon");
}
如何获取book对象的所有实例变量 我希望结果变成 - > 1,“新月” 不使用getter方法,所以我可以转换其他POJO对象。
澄清:
我有2个班级
Book {
String id;
String title;
//constructor
//setter
}
Student {
String id;
String name;
//cuonstructor
//setter
}
main() {
Book book = new Book();
book.setId(1);
book.setTitle("new moon");
Student student = new Student();
student.setId(1);
student.setName("andrew");
//suppose i have BeanUtil object to get all instance varable value and class meta data
BeanUtil.getMetadata(book, Book.class);
//output is
//id, title
//suppose i have BeanUtil object to get all instance varable value and class meta data
BeanUtil.getMetadata(student, Students.class);
//output is
//id, name
BeanUtil.getInstanceVariableValue(student, Student.class);
//output
//1, andrew
BeanUtil.getInstanceVariableValue(book, Book.class);
//output
//1, new moon
}
答案 0 :(得分:3)
我通常使用PropertyUtils,BeanUtils。
//get all of the properties for a POJO
descriptors = PropertyUtils.getPropertyDescriptors(book);
//go through all values
Object value = null;
for ( int i = 0; i < descriptors.length; i++ ) {
value = PropertyUtils.getSimpleProperty(bean, descriptors[i].getName())
}
//copy properties from POJO to POJO
PropertyUtils.copyProperties(fromBook, toBook);
答案 1 :(得分:1)
如果要获取Book实例的所有属性的值,可以使用反射执行此操作。但是,这需要大量代码,并且价格昂贵。更好的方法是(IMO)简单地实现getAllValues()
方法:
public Object[] getAllValues() {
return new Object[]{this.id, this.title};
}
或者更好的是,让它填充并返回Map或Properties对象。我想这取决于你的用例哪个更好。 (虽然我很难理解为什么你想要数组/列表中所有属性的值...)
答案 2 :(得分:1)
这个怎么样:
public static String getMetadata(Class input) {
StringBuffer result = new StringBuffer();
// this will get all fields declared by the input class
Field[] fields = input.getDeclaredFields();
for (int i=0; i<fields.length; i++) {
if (i > 0) {
result.append(", ");
}
field[i].setAccessible(true);
result.append(field[i].getName());
}
}
public static String getInstanceVariableValue(Object input) {
StringBuffer result = new StringBuffer();
// this will get all fields declared by the input object
Field[] fields = input.getClass().getDeclaredFields();
for (int i=0; i<fields.length; i++) {
if (i > 0) {
result.append(", ");
}
fields[i].setAccessible(true);
result.append(fields[i].get(input));
}
return result;
}
我没有尝试编译或运行它,所以让我知道它是怎么回事
答案 3 :(得分:1)
听起来像你的项目(我假设一个家庭作业项目?)是要学习Reflections。
答案 4 :(得分:0)
public class Book {
private String id;
private String title;
public Book(String id, String title) {
this.id = id;
this.title = title;
}
// Setters and getters...
}
public class Library {
private Map<String, Book> booksByTitle = new HashMap<String, Book>();
public Library() {
}
public void addBook(Book book) {
this.booksByTitle.put(book.getTitle(), book);
}
public Book getBookByTitle(String title) {
// Returns null if no matching entry is found in the map.
return this.booksByTitle.get(title);
}
public static void main(String args[]) {
Library myLibrary = new Library();
myLibrary.addBook(new Book("1", "new moon"));
myLibrary.addBook(new Book("2", "fight club"));
Book book = myLibrary.getBookByTitle("new moon");
if (book == null) {
// A book by that title is not in the library
}
}
}
答案 5 :(得分:0)
我认为adisembiring要问的是如何在不调用每个实例变量的单个getter的情况下获取其POJO中的值。有一种方法可以使用反射来做到这一点。 Here是一篇关于如何创建toString()方法的文章,从Example#2开始,它使用Reflection API动态确定要输出的内容。