方法调用中的Java 7 Diamond操作 [英] Java 7 Diamond Operation in method call

查看:132
本文介绍了方法调用中的Java 7 Diamond操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于讨论的后续问题:

This is kind of a follow up question on the discussion:

为什么不这样做? Diamond运算符在Java 7中的addAll()调用中工作吗?

在Java教程中,

http://docs.oracle.com/javase/tutorial /java/generics/gentypeinference.html

请注意,菱形通常在方法调用中起作用;但是,为更清楚起见,建议您主要使用菱形在声明它的位置初始化变量

Note that the diamond often works in method calls; however, for greater clarity, it is suggested that you use the diamond primarily to initialize a variable where it is declared

所以,我对第一行有些困惑. 钻石何时在方法调用中起作用?

So, I am a bit confused about the first line. When does diamond work in method calls?

有关钻石算子如何工作的更多解释可以在这里找到:

A bit more explanation on how diamond operator works can be found here:

http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#What%20is%20type%20argument%20inference%20for%20constructors ?

然后,我尝试了以下方法,效果很好:

And from this, I have tried the following, which works fine:

请允许我拥有

private static class Box<T>{
    public Box(T t){}
}
static void f(Box<Integer> box){}

如下所示的调用可以正常编译:

a call like the following compiles fine:

f(new Box<>(new Integer(10)));

在上述f()的方法调用中调用构造函数的type参数是从该参数推断到构造函数的(即Integer).

The type parameter in invoking the constructor in the method call of f() above is inferred from the argument to the constructor (i.e. Integer).

这就是本教程所说的

请注意,菱形通常在方法调用中起作用

Note that the diamond often works in method calls

如果没有,那么谁能提供一个示例,说明钻石在方法调用中起作用?

If not, can anyone kind enough to provide an example where diamond works in method call?

推荐答案

这就是本教程所说的意思

So is this what is meant when the tutorial says

我认为是的,尽管涉及到<>运算符时有一些陷阱.

I think yes though there are a couple of gotchas when it comes to <> operators.

在您的情况下,Box实例化不是问题,因为可以使用构造函数参数轻松推断类型.尝试将构造函数更改为"not"以使用IntegerT并查看调用如何失败.

In your case, Box instantiation is a non-issue given that the type can be trivially inferred using the constructor argument. Try changing the constructor to "not" take in an Integer or T and see how the invocation fails.

class BadBox<T> {

    private T t;

    public BadBox(){}    

    public void setT(T t) {
        this.t = t;
    }

    static void f(BadBox<Integer> box){}

    public static void main(final String[] args) {
        f(new BadBox<>());  //fails, should have worked ideally
    }    
}

类似地,看看这堂课:

class Testi<R> {    
    public void doIt(Set<? extends R> sets) {
    }

    public static void main(final String[] args) {
            // works since type inference is now possible
        new Testi<CharSequence>().doIt(new HashSet<>(Arrays.asList("a")));

            // fails; nothing which can help with type inference
        new Testi<CharSequence>().doIt(new HashSet<>();
    }       
}

类似地,可以通过如下帮助编译器来简单地解决链接的问题(关于addAll)中的问题:

Similarly, the problem in your linked question (regarding addAll) can be simply solved by helping out the compiler a bit as follows:

List<String> list = new ArrayList<>();
list.add("A");

// works now! use only if you love diamond operator ;)
list.addAll(new ArrayList<>(Arrays.asList(new String[0])));
// or the old-school way
list.addAll(new ArrayList<String>()));

在实现匿名类方面,Diamond运算符似乎也中断了:

Diamond operators also seem to break when it comes to implementing anonymous classes as follows:

final String[] strings = { "a", "b", "c" };
Arrays.sort(strings, new Comparator<>() {
    @Override
    public int compare(String o1, String o2) {
        return 0;
    }
});

幸运的是,在这种情况下,编译器非常明确地提到<>不适用于匿名类.

Fortunately, in this case, the compiler is pretty explicit in mentioning that <> doesn't/won't work with anonymous classes.

这篇关于方法调用中的Java 7 Diamond操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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