我浏览了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);
我检查了这里的实现:https://code.google.com/p/google-collections/source/browse/trunk/src/com/google/common/collect/ImmutableSet.java
有一个具有以下签名的 create 方法:
private static <E> ImmutableSet<E> create(E... elements)
它包裹着
private static <E> ImmutableSet<E> create(Iterable<? extends E> iterable, int count);
方法。公共方法只是将参数传递给 create(E... elements) 签名方法,该方法最终调用另一个 create 方法。
我猜想具有固定参数数量的方法的 public 是不必要的,因为我们有 of(E... elements) 方法。
我的问题是他们为什么这样做?表现?或者它是一种模式?
Thanks.