I have several objects that look like this:
class PhoneNumber
{
String getNumber();
String getExtension();
DateTime getLastCalled();
}
class Address
{
String getCity();
string getState();
int getZip();
}
I would like to be able to take a list of any of these objects and get a list of a specific property. This is a trivial operation if I were to write a function for each property, but as I understand it, it is not good practice to duplicate code so many times.
Therefore, I would like to be able to do something similar for any property in any object:
List<Address> AddressList = getAddresses();
List<String> CityList = getListOfProperties( AddressList, Address.getCity )
I struggled to do this with generics. I'm not even sure that this is possible, but as I understand it, you can do a lot of magical things with the Java programming language.