mardi 21 juin 2016

loop through field's values (or methods) of an object in java

I have a class called Material and some subclasses of it: Book, Audio, magazine... I also have an ArrayList<Material> arrayOfMaterials

I am trying to implement a search method: I want to give a string to this method as a parameter and it should call all the getters() in every object inside the array, plus the toString functions and compare the results to the input parameter.

I have read about Reflection in java, but if I got it right, that will give me the names of the fields or methods of a class, but won't give me the values of those fields in objects or the results of calling its getters().

I need to loop through ALL getters, or through all fields (I shouldn't access fields from outside, but it'd work), and not all subclasses of Materialhave the same number of fields.

Is there any way to do this? I have no code to paste because, as far as I know, there is no way to do this.





Aucun commentaire:

Enregistrer un commentaire