为什么Guava类提供了如此多的工厂方法而不是只提供varargs的方法? [英] Why do Guava classes provide so many factory methods instead of just one that takes varargs?
问题描述
查看Guava的 ImmutableList (以及其他一些类),你会发现很多重载的
便利方法(退货一个包含给定元素的不可变列表,按顺序。)使用不同数量的参数:
Looking at Guava's ImmutableList (and some other classes), you'll find plenty of overloaded of
convenience methods ("Returns an immutable list containing the given elements, in order.") that take a different number of parameters:
...
public static <E> ImmutableList<E> of(E e1, E e2, E e3)
public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4)
public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5)
...
一直到这个:
public static <E> ImmutableList<E> of(E e1,
E e2,
E e3,
E e4,
E e5,
E e6,
E e7,
E e8,
E e9,
E e10,
E e11,
E e12,
E... others)
我的一些同事认为这很愚蠢,想知道为什么不只有一种方法: of(E ..元素)
。他们怀疑这是一个不良导向的表现优化,属于你认为你比编译器更聪明,或类似的东西。
Some colleagues of mine consider this silly, wondering why there's isn't just one method: of(E... elements)
. They suspect it's an ill-guided performance "optimisation" that falls into the category "do you think you're smarter than compiler", or something like that.
我的预感就是Kevin Bourrillion等人。把这些方法放在那里是出于真正的原因。任何人都可以解释(或推测)这个原因是什么吗?
My hunch is that Kevin Bourrillion et al. put these methods in there for a real reason. Can anyone explain (or speculate) what that reason might be?
推荐答案
来源说:
// These go up to eleven. After that, you just get the varargs form, and
// whatever warnings might come along with it. :(
所以,这是因为varargs方法产生带有泛型参数的警告。
So, this was done because varargs methods produce a warning with generic arguments.
这篇关于为什么Guava类提供了如此多的工厂方法而不是只提供varargs的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!