这个Java通用运算符是什么;x、 <;y>;功能(…)”等;打过电话,它做什么?

这个Java通用运算符是什么;x、 <;y>;功能(…)”等;打过电话,它做什么?,java,android,generics,Java,Android,Generics,我很难给这个问题一个恰当的标题,但我已经阅读了一些源代码,并找到了以下代码片段 return new IdpResponse( in.<User>readParcelable(User.class.getClassLoader()), // <-- x.<y>function(...) in.readString(), in.readString(), in.readInt() == 1, (FirebaseUiExcepti

我很难给这个问题一个恰当的标题,但我已经阅读了一些源代码,并找到了以下代码片段

return new IdpResponse(
    in.<User>readParcelable(User.class.getClassLoader()), // <-- x.<y>function(...)
    in.readString(),
    in.readString(),
    in.readInt() == 1,
    (FirebaseUiException) in.readSerializable(),
    in.<AuthCredential>readParcelable(AuthCredential.class.getClassLoader()) // <-- x.<y>function(...)
);

返回新的响应(

在.readParcelable(User.class.getClassLoader())中,//回答您的问题:

  • 正如Albertosinagalia和Pshemo所说,您所问的结构:

    .readParcelable(User.class.getClassLoader())中的

    in

  • 您可以在Oracle的Java教程中阅读更多详细信息:

    • …和
  • 引用“类型推断”教程:

  • 类型推断是Java编译器查看每个方法调用和相应声明以确定类型的能力 使调用适用的一个或多个参数

    考虑以下示例BoxDemo,它需要Box类:

    public class BoxDemo {
    
      public static <U> void addBox(U u, 
          java.util.List<Box<U>> boxes) {
        Box<U> box = new Box<>();
        box.set(u);
        boxes.add(box);
      }
    
      public static <U> void outputBoxes(java.util.List<Box<U>> boxes) {
        int counter = 0;
        for (Box<U> box: boxes) {
          U boxContents = box.get();
          System.out.println("Box #" + counter + " contains [" +
                 boxContents.toString() + "]");
          counter++;
        }
      }
    
      public static void main(String[] args) {
        java.util.ArrayList<Box<Integer>> listOfIntegerBoxes =
          new java.util.ArrayList<>();
        BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
        BoxDemo.addBox(Integer.valueOf(20), listOfIntegerBoxes);
        BoxDemo.addBox(Integer.valueOf(30), listOfIntegerBoxes);
        BoxDemo.outputBoxes(listOfIntegerBoxes);
      }
    }
    
  • 最后,关于“它看起来几乎像是对函数结果的转换”:
  • 泛型的要点是不允许类使用不同的类型 同时,

    泛型允许您定义/限制的实例所使用的类型 物体

    泛型背后的思想是消除强制转换的需要


    这是一个带模板的函数,看看这个你熟悉吗?语法
    .someGenericMethod()< /代码>您只需明确地指出在当前方法调用中泛型类型应该代表的特定类型。@ pSHIMO -谢谢,就是这样。C++中的函数模板类似于它。通常称为“代码>类型见证< /COD>,当类型推断不能自己确定类型时,需要使用模板。不,它叫a,不是“函数”,不是“模板”。感谢您提供Java规范中使用的术语(毕竟,OP的问题是“它叫什么?”)。不过,非正式地说,我毫不犹豫地说它像“函数模板”(Albertosinaglia称之为),甚至是“子程序模板”(术语没有任何Java含义)。我希望我至少成功地传达了DanielF询问的概念。
    
    public class BoxDemo {
    
      public static <U> void addBox(U u, 
          java.util.List<Box<U>> boxes) {
        Box<U> box = new Box<>();
        box.set(u);
        boxes.add(box);
      }
    
      public static <U> void outputBoxes(java.util.List<Box<U>> boxes) {
        int counter = 0;
        for (Box<U> box: boxes) {
          U boxContents = box.get();
          System.out.println("Box #" + counter + " contains [" +
                 boxContents.toString() + "]");
          counter++;
        }
      }
    
      public static void main(String[] args) {
        java.util.ArrayList<Box<Integer>> listOfIntegerBoxes =
          new java.util.ArrayList<>();
        BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
        BoxDemo.addBox(Integer.valueOf(20), listOfIntegerBoxes);
        BoxDemo.addBox(Integer.valueOf(30), listOfIntegerBoxes);
        BoxDemo.outputBoxes(listOfIntegerBoxes);
      }
    }
    
    BoxDemo.<Integer>addBox(Integer.valueOf(10), listOfIntegerBoxes);
    
    BoxDemo.addBox(Integer.valueOf(20), listOfIntegerBoxes);