I have a class containing 2 lists, of different object types.
List<Student> students;
List<Lecturer> lecturers;
Each of these classes have a method that returns a String. I want to find something from that String and the method that I would create would be the same for both lists,the class name(Student/Lecturer), the list name(students/lecturers) and the method name(studentMethod/lecturerMethod) being the only ones that differ.
Knowing the className, listName and the methodName, how can I get the proper list(called listName) so I can then call the desired method(called methodName) from each object in the list.
P.S. : I know it would be easier to just create 2 separate methods for each list but I want my code to be DRY and also to learn more about OOP principles.
Example: If I want to get the students list and then invoke the studentMethod for each student I should have something like this:
void dryMethod(String className, String listName, String methodName) {
Class<?> desiredClass = Class.forName(className);
List<desiredClass> desiredList = getListByName(listName);
Method desiredMethod = getMethodByName(methodName);
for(desiredClass object : desiredList){
manipulateString(object.desiredMethod());
}
}
dryMethod("Student","students","studentMethod");
Is it possible?
Aucun commentaire:
Enregistrer un commentaire