在返回集合的接口中使用 Java 泛型。最佳实践?陷阱?
我今天遇到了一些我觉得有问题的代码。这是一个简化的示例(不现实)。
public interface IListable {
//returns first n items from list
public ArrayList getFirstNThings(int n);
//returns last n items from list
public ArrayList getLastNThings(int n);
}
然后有一个像这样的实现者:
public GroceryList implements IListable {
private ArrayList<GroceryItem> groceries;
public GroceryList() {
this.groceries = new ArrayList<GroceryItem>();
}
public ArrayList<GroceryItem> getFirstNThings(int n) {
ArrayList<GroceryItem> firstNThings = new ArrayList<GroceryItem>();
for (int i=0; i < n; i++) {
firstNThings.add(this.groceries.get(i));
}
return firstNThings
}
public ArrayList<GroceryItem> getLastNThings(int n) {
ArrayList<GroceryItem> lastNThings = new ArrayList<GroceryItem>();
for (int i=this.groceries.size(); i < this.groceries.size()-n; i--) {
lastNThings.add(this.groceries.get(i-1);
}
return lastNThings;
}
}
忽略您可能在其中发现的任何实现问题(我也发现了一些)。我的意思是,该接口不使用 ArrayList 的任何泛型类型参数(即 ArrayList),但该接口方法的实现者却使用了任何通用类型参数(即 ArrayList
所以我的问题是:这是一个问题吗?我应该重构什么吗?值得吗?有什么好处?如果我在接口中定义了一个方法,其返回类型是原始类型,但该方法的实际实现者返回各种参数化类型,我会遇到什么样的问题?
I ran into some code today that I found questionable. Here's a simplified example (not realistic).
public interface IListable {
//returns first n items from list
public ArrayList getFirstNThings(int n);
//returns last n items from list
public ArrayList getLastNThings(int n);
}
Then there's an implementor like so:
public GroceryList implements IListable {
private ArrayList<GroceryItem> groceries;
public GroceryList() {
this.groceries = new ArrayList<GroceryItem>();
}
public ArrayList<GroceryItem> getFirstNThings(int n) {
ArrayList<GroceryItem> firstNThings = new ArrayList<GroceryItem>();
for (int i=0; i < n; i++) {
firstNThings.add(this.groceries.get(i));
}
return firstNThings
}
public ArrayList<GroceryItem> getLastNThings(int n) {
ArrayList<GroceryItem> lastNThings = new ArrayList<GroceryItem>();
for (int i=this.groceries.size(); i < this.groceries.size()-n; i--) {
lastNThings.add(this.groceries.get(i-1);
}
return lastNThings;
}
}
Ignore any implementation problems you may find in that (I found a few too). What I'm getting at is that the interface does not use any generic type parameter for ArrayList (i.e ArrayList<?>), but the implementor of the interface's method does (i.e. ArrayList<GroceryList>). Other implementors may return ArrayLists with any other type parameters, no?
So my questions: Is this a problem? Should I refactor anything? Is it worth it? What's the advantage? What kind of problems can I run into if I have a method defined in an interface whose return type is a raw type, but the actual implementors of the method return various parameterized types?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 IListable 的两种方法始终返回相同的类型,请改用此方法:
如果这不是一个选项,请尝试使用 ?反而。虽然它基本上是相同的,但它避免了难看的警告。
一般来说,在实现中使用比超类型或接口更具体的返回类型不是问题。如果您正在处理 IListable,则需要处理返回列表中的任何对象类型。如果您正在处理 GroceryList,则您只需要 GroceryItems。这不仅适用于返回类型的通用类型参数,也适用于返回类型本身。因此,如果接口指定
List; get()
,可以将其实现为ArrayList;获取()。
if both methods of IListable always return the same type, use this instead:
if this isn't an option, try using ? instead. While it's basically the same, it avoids ugly warnings.
Generally, it's not a problem to use a more specific return type in an implementation than in a super-type or interface. If you're dealing with IListable, you need to handle any object type in the returned list. If you're dealing with GroceryList, you expect only GroceryItems. That's not only true for genric type arguments of return types, but for the return type itself as well. So if an interface specifies
List<Foo> get()
, is okay to implement it asArrayList<Foo> get()
.最佳实践是永远不要在 Open 代码中返回带有 pure 通配符的
List
,就像不应该返回一样空
。Java 中的通配符泛型会给所有使用你的代码的程序员带来污染。您可以在Close代码中执行此操作来解决本地问题。
通常,您将避免返回协变和逆变通配符,例如
List
和List
如果您知道自己在做什么并阅读有关 PECS 和有界通配符。 不要仅仅因为它可以编译就这样做。
Best practice is to never return a
List<?>
with pure wildcard in your Open code, just like you should not returnnull
.Wildcard generics in Java will bring contamination to all coders using your code. You can do it in your Close code to resolve local problem.
Usually, you will avoid to return covariance and contravariance wildcards like
List<? extends User>
andList<? super User>
You can do it if you know what you are doing and read everything about PECS, and bounded wildcards. Don't do it only because it compiles.