等价方法重载为何必要? [英] Equivalient method overload why necessary?

查看:57
本文介绍了等价方法重载为何必要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了Google制作的一些JAVA代码,并找到了ImmutableSet:

I browsed some JAVA code made by Google, and I found the ImmutableSet: http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/ImmutableSet.html

他们以其他几种方式实现了of()方法:

They implemented the of() method with several other ways:

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);

我检查了以下实施:有一个带有以下签名的create方法:

There's a create method wiith the following signature:

private static <E> ImmutableSet<E> create(E... elements)

包裹

private static <E> ImmutableSet<E> create(Iterable<? extends E> iterable, int count);

方法.公共方法只是将参数传递给签名的create(E ... elements)方法,该方法最终调用另一个create方法.

method. The public methods just passes the parameters to the create(E... elements) signatured method which finally calls the other create method.

我猜想,因为我们有了of(E ... elements)方法,所以没有必要公开带有固定参数数量的方法.

I guess that the public of methods with fixed count of parameter are unnecessary since we have the of(E... elements) method.

我的问题是,为什么他们这样做?表现?还是一种模式?

My question is that why did they do that like this? Performance? Or it's a pattern?

谢谢.

推荐答案

实际上,它与性能无关:所有方法都委托给同一个创建方法,而该方法总是需要一个数组.

It can't be related to performance, actually: All the methods delegate to the same creation method, which expects an array anyhow.

我的猜测是它与警告有关.考虑以下最小片段:

My guess is that it is related to warnings. Consider the following, minimal snippet:

import java.util.List;

class ImmutableSet<T>
{
}
public class ParametersTest
{
    public static void main(String[] args)
    {
        List<String> list0 = null;
        List<String> list1 = null;
        of(list0, list1);
    }

    @SuppressWarnings("unchecked")
    public static <E> ImmutableSet<E> of(E e1, E e2) {
        return create(e1, e2);
    }

    public static <E> ImmutableSet<E> of(E... elements) {
        return create(elements);
    }

    private static <E> ImmutableSet<E> create(E... elements) 
    {
        return null;
    }

}

在main方法中对 of 的调用很好:它与 of 方法的2-args版本匹配.现在,注释掉 of 方法的2-args版本.然后,调用仍然可以,但是将直接调用varags版本.这将导致创建通用数组,并引起警告.(显然,该警告在2-args-version中已被取消.)

The call to of in the main method is fine: It matches the 2-args-version of the of method. Now comment out the 2-args-version of the of-method. Then the call is still OK, but will directly invoke the varags version. This will cause a generic array to be created, and cause a warning. (This warning is suppressed in the 2-args-version, obviously).

因此,总而言之,我认为这是为了避免对要使用通用类型的多个对象调用 of 方法的库客户端发出警告.

So to summarize, I assume that this is in order to avoid warnings for clients of the library who want to invoke the of method with several objects of a generic type.

幸运的是,由于有了 http://docs.oracle.com/javase/7/docs/api/java/lang/SafeVarargs.html

这篇关于等价方法重载为何必要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆