我只是看着番石榴ImmutableList,我注意到这个of()方法超载了12次.
在我看来,他们所需要的只是:
static <E> ImmutableList<E> of();
static <E> ImmutableList<E> of(E element); // not even necessary
static <E> ImmutableList<E> of(E... elements);
Run Code Online (Sandbox Code Playgroud)
有这么多类似变化的原因是什么?
我浏览了Google制作的一些JAVA代码,我找到了ImmutableSet:http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/ImmutableSet.html
他们用其他几种方法实现了of()方法:
public static <E> ImmutableSet<E> of(E e1, E e2);
public static <E> ImmutableSet<E> of(E e1, E e2, E e3);
public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4);
public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4, E e5);
public static <E> ImmutableSet<E> of(E... elements);
Run Code Online (Sandbox Code Playgroud)
我查看了此处的实施:https://code.google.com/p/google-collections/source/browse/trunk/src/com/google/common/collect/ImmutableSet.java
有一个创建方法,具有以下签名:
private static <E> ImmutableSet<E> create(E... elements)
Run Code Online (Sandbox Code Playgroud)
包裹了
private static <E> ImmutableSet<E> create(Iterable<? extends E> iterable, int count);
Run Code Online (Sandbox Code Playgroud)
方法.public方法只是将参数传递给create(E ... elements)signatured方法,该方法最终调用另一个create方法. …
我有疑问:这两个声明有什么区别?
public static void printMax(double... numbers) { ... }
public static void printmax(double numbers[]) { ... }
Run Code Online (Sandbox Code Playgroud)
是double... numbers一样的double numbers[]吗?