调用java通用方法 [英] Invocation of java generic method

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

问题描述

给定泛型方法:

 < T>列表与LT; T> getGenericList(int i){...} 

以下代码无任何警告地编译:

  public List< String> getStringList(boolean b){
if(b)
return getGenericList(0);
else
return getGenericList(1);
}

但是这个会产生'Type mismatch'编译错误:

  public List< String> getStringList(boolean b){
return(b)? getGenericList(0):getGenericList(1);
}

为什么?

解决方案

这是不是泛型问题,而是编译器推断三元表达式类型的结果。



它与这个等效代码发生相同。这段代码的工作原理如下:

  public byte function(boolean b){
if(b)
return 1 ;
else
return 2;
}

虽然这不是:

  public byte function(boolean b){
return(b)? 1:2;

$ / code>

原因是当编译器试图推断这个表达式的类型时 p>

  return(b)? 1:2; 

首先必须获取每个操作数的类型,并检查它们是否兼容( 参考)来评估三元表达式是否有效。 如果返回类型被传播以自动投射或提升每个操作数,则可能导致根据上下文不同地解析三元表达式的类型>这个表达式。

鉴于返回的类型不能传播到操作数,那么在修改的情况下:

  return(b)? getGenericList(0):getGenericList(1); 

通用类型的绑定无法完成,因此类型每个操作数都被解析为 List< Object> 。然后编译器得出结论,整个表达式的类型是 List< Object> ,它不能被自动转换为 List< Integer>



 <$ c $>(因为它们不是兼容类型)。 code> return getGenericList(0); 

它将返回的类型应用于绑定泛型类型T,因此编译器得出结论:表达式有一个 List< String> 类型,可以安全地返回。


Given the generic method:

<T> List<T> getGenericList(int i) {...}

the following code compiles without any warning:

public List<String> getStringList(boolean b){
    if(b)
        return getGenericList(0);
    else
        return getGenericList(1);
}

but this one generates 'Type mismatch' compilation error:

public List<String> getStringList(boolean b) {
    return (b) ? getGenericList(0) : getGenericList(1);
}

Why?

解决方案

This is NOT a generics problem, but a consequence of the way the compiler has to infer the type of the ternary expression.

It happens the same with this equivalent code. This code works:

public byte function(boolean b){
    if(b)
        return 1;
    else
        return 2;
}

While this doesn't:

public byte function(boolean b) {
    return (b) ? 1 : 2;
}

The reason is that when the compiler tries infer the type of this expression

    return (b) ? 1 : 2;

It first has to obtain the type of each one of the operands, and check if they are compatible (reference) to evaluate wether the ternary expression is valid or not. If the type of the "return" were propagated to automatically cast or promote each one of the operands, it could lead to resolve the type of a ternary expression differently depending on the context of this expression.

Given that the type of the "return" cannot be propagated to the operands, then in the menctioned case:

    return (b) ? getGenericList(0) : getGenericList(1);

the binding of the generic type cannot be done, so the type of each one of the operands is resolved to List<Object>. Then the compiler concludes that the type of the whole expression is List<Object>, which cannot be automatically casted to List<Integer> (because they are not compatible types).

Whereas this other one

    return getGenericList(0);

It applyes the type of the "return" to bind the generic type T, so the compiler concludes that the expression has a List<String> type, that can be returned safely.

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

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