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オブジェクトの全インスタンス変数を取得する方法 getterメソッドを使わずに結果を→1、「新月」にしたいので、他の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

私は通常、 BeanUtilsの一部であるPropertyUtilsを使用します。

//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

解体が求めているのは、インスタンス変数ごとに個別のゲッターを呼び出さずに、POJOで値を取得する方法だと思います。リフレクションを使用してこれを行う方法があります。これは、toString()メソッドを作成する方法に関する記事であり、例2から始めて、ReflectionAPIを使用して出力するものを動的に決定します。

于 2009-12-08T03:33:31.537 に答える
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 に答える