使用不同的T类调用print(List <T> a,T b)时发生错误 [英] Errors occur when calling print(List&lt;T&gt; a, T b) with different T class

查看:89
本文介绍了使用不同的T类调用print(List <T> a,T b)时发生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public static< T>< p> void print(T a,T b){
System.out.println(a);
System.out.println(b);
}

public static void main(String [] args){
print(new ArrayList< String>(),1);
}

其中没有问题。

然而,当我将 print 方法更改为以下内容时,它会给我编译错误。

  public static< T> void print(List< T> a,T b){
System.out.println(a);
System.out.println(b);

错误:

<$ GenericTest.java:9:错误:类GenericTest中的方法打印不能应用于给定的类型;
print(new ArrayList< String>(),1);
$
required:List< T>,T
found:ArrayList< String>,int
reason:没有类型变量(s)T的实例存在,参数类型int符合形式参数类型T
其中T是类型变量:
T extends在方法< T> print(List< T> T)中声明的对象
1错误

任何人都可以帮我理解错误吗?

解决方案

首先您应该了解的是,使用以下方法签名

 公共静态< T> void print(T a,T b)



T 必须是相同的类型,也就是说 a b 将会有相同的传染类型。

为什么它对新的ArrayList< String>() 1 ?因为这两个参数实际上可以表示为 Serializable ,它是最接近的常见超类型 ArrayList 整数



  • ArrayList 实现 Serializable 接口。 / li>
  • 1 可以装入 Integer ,它也是 Serializable

    所以在这种情况下,编译器会推断出 T 作为 Serializable






    在第二种情况下,

      public static< T> void print(List  a,T b)

    没有常见的超类型 T List< String> 整数都是有效的。确实, String Integer 都是 Serializable ,但由于泛型不是多态,它不起作用。


    I am trying to learn Java Generics, and found the following code.

    public static <T> void print(T a, T b){
        System.out.println(a);
        System.out.println(b);
    }
    
    public static void main(String[] args){
        print(new ArrayList<String>(), 1);
    }
    

    Which works with no problem.

    However when I change print method to the following, it gives me compiling errors.

    public static <T> void print(List<T> a, T b){
        System.out.println(a);
        System.out.println(b);
    }
    

    Error:

    GenericTest.java:9: error: method print in class GenericTest cannot be applied to given types;
      print(new ArrayList<String>(), 1);
        ^
      required: List<T>,T
      found: ArrayList<String>,int
      reason: no instance(s) of type variable(s) T exist so that argument type int conforms to formal parameter type T
      where T is a type-variable:
        T extends Object declared in method <T>print(List<T>,T)
    1 error
    

    Can anyone help me understand the errors?

    解决方案

    The first thing you should understand is that, with the following method signature

    public static <T> void print(T a, T b)
    

    Both T must be the same type, that is to say both a and b will have the same infered type.

    So why does it work for new ArrayList<String>() and 1? Because both parameters can actually be represented as Serializable, which is the nearest common super type of ArrayList and Integer:

    • ArrayList implements the Serializable interface.
    • 1 can be boxed into an Integer, which is also Serializable.

    So in this case, the compiler will infer T as Serializable.


    In the second case, with the signature

    public static <T> void print(List<T> a, T b)
    

    There is no common super type T that would be valid for both List<String> and Integer. It is true that both String and Integer are Serializable, but since generics aren't polymorphic, it doesn't work.

    这篇关于使用不同的T类调用print(List <T> a,T b)时发生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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