0

我有 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
}
4

6 回答 6

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);
于 2009-12-08T03:39:20.117 回答
1

如果要获取 Book 实例的所有属性的值,可以使用反射来完成。但是,这将需要大量代码,而且成本很高。更好的方法是(IMO)简单地实现一个getAllValues()方法:

public Object[] getAllValues() {
    return new Object[]{this.id, this.title};
}

或者更好的是,让它填充并返回一个 Map 或一个 Properties 对象。我想这取决于你的用例哪个更好。(虽然我很难理解为什么你会想要一个数组/列表中所有属性的值......)

于 2009-12-08T03:37:00.550 回答
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;
}

我没有尝试编译或运行它,所以让我知道它是怎么回事

于 2009-12-08T04:26:19.533 回答
1

听起来你的项目(我假设一个家庭作业项目?)的重点是学习Reflections

于 2009-12-08T05:48:44.503 回答
0

像迈克尔一样,我不确定我是否完全理解你的问题。听起来您希望能够在给定包含其标题的字符串的情况下获取 Book 对象。假设你已经像上面那样声明了你的 Book 类,试试这个:

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
        }
    }
}
于 2009-12-08T03:23:56.887 回答
0

我认为 adisembiring 所问的是如何在他的 POJO 中获取值,而无需为每个实例变量调用单独的 getter。有一种方法可以使用反射来做到这一点。是一篇关于如何创建 toString() 方法的文章,从示例 #2 开始,它使用反射 API 来动态确定要输出的内容。

于 2009-12-08T03:33:31.537 回答