我有几个看起来像这样的对象:
class PhoneNumber
{
String getNumber();
String getExtension();
DateTime getLastCalled();
}
class Address
{
String getCity();
string getState();
int getZip();
}
我希望能够获取其中任何一个对象的列表并获取特定属性的列表。如果我要为每个属性编写一个函数,这是一个微不足道的操作,但据我所知,多次重复代码是不好的做法。
所以我希望能够为任一对象中的任何属性做这样的事情:
List<Address> AddressList = getAddresses();
List<String> CityList = getListOfProperties( AddressList, Address.getCity /*<--Instruction as to what property to populate the returned list with */ )
我绞尽脑汁试图用泛型来做到这一点。我什至不确定这是否可能,但据我了解,使用 Java 编程语言可以完成很多神奇的事情。