Java从对象列表中获取每个对象中的属性列表时防止代码重复
我有几个如下所示的对象:
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 编程语言可以完成很多神奇的事情。
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'd like to be able to take a List of any one of those objects and get a list of a particular property. This is a trivial operation if I were to write a function for each property, but as I understand it, it is bad practice to duplicate code so many times.
So I would like to be able to do something like this for any property in either object:
List<Address> AddressList = getAddresses();
List<String> CityList = getListOfProperties( AddressList, Address.getCity /*<--Instruction as to what property to populate the returned list with */ )
I've racked my brain trying to do this with generics. I'm not even sure that this is possible, but as I understand it, a lot of magical things can be done with the Java programming language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用泛型和实用程序类来完成这项工作。我比使用反射更喜欢这个,因为您可以在编译时检查您正在检索的属性是否存在(并且没有拼写错误等)。
首先是一个执行实际转换的类。请注意,当我们调用 getListOfProperties() 时,我们将在匿名内部类中重写抽象“get”方法。
然后你就这样使用它。不是一个漂亮的衬里,但编译时间检查良好:
You can do this with generics and a utility class to do the work. I like this a little bit better than using reflection because you get compile time checking that the property you're retrieving exists (and isn't misspelled etc.).
First a class that does the actual conversion. Note the abstract "get" method which we'll override in an anonymous inner class when we call getListOfProperties().
Then you use it like this. Not a pretty one liner, but compile time checking goodness:
对于如此简单的事情,您最好只提供 getter 和 setter。拒绝重复一行代码并不能节省太多。更好的是,只需让您的 IDE 为您编写即可。
For something this simple, you're better off just providing the getters and setters. You're not saving much by refusing to duplicate one line of code. Even better, just get your IDE to write it for you.
您可以使用
Introspector 处理对象
就像这个答案< /a>.然后你可以像这样调用:
You could handle your objects with
Introspector
like in this SO answer.Then you can do a call like this:
您可以使用反射(java.lang.Class和java.lang.reflect.*)来获取方法名称;以“get”开头的是属性,其名称遵循“get”。
You can use reflection (java.lang.Class and java.lang.reflect.*), to get method names; those beginning with "get" are properties, whose name follows "get".
这是 Java 中闭包的一个很好的例子;它即将到来,但尚未到来。
同时,您可以通过反射来做到这一点,尽管反射总是会带来性能损失。反射可以做的事情之一是让您通过将方法/属性的名称指定为字符串来调用对象的方法或检索属性;在您的情况下,您需要执行 getListOfProperties(AddressList, "city") 并在该方法中使用反射来检索指定的属性。
通过使用 Apache Commons BeanUtils 库,这种类型的事情得到了极大的简化。
This is a pretty good case for closures in Java; it's coming, but it's not here yet.
In the meantime, you can do this with reflection, although reflection always has a performance penalty. One of the things reflection can do is to let you call a method on an object, or retrieve a property, by specifying the name of the method/property as a string; in your case you would want to do
getListOfProperties(AddressList, "city")
and within that method use reflection to retrieve the specified property.This type of thing is simplified dramatically by using the Apache Commons BeanUtils library.