简体   繁体   English

Java:什么是 - public static <T> foo(){...}?

[英]Java : What is - public static<T> foo() {…}?

I saw a java function that looked something like this- 我看到一个看起来像这样的java函数 -

public static<T> foo() {...}

I know what generics are but can someone explain the in this context? 我知道什么是泛型,但有人可以解释这个背景吗? Who decides what T is equal to? 谁决定T等于什么? Whats going on here? 这里发生了什么?

EDIT: Can someone please show me an example of a function like this. 编辑:有人可以给我看一个像这样的功能的例子。

You've missed the return type out, but apart from that it's a generic method. 你错过了返回类型,但除此之外它是一种通用的方法。 As with generic types, T stands in for any reference type (within bounds if given). 与泛型类型一样, T代表任何引用类型(如果给定,则在范围内)。

For methods, generic parameters are typically inferred by the compiler. 对于方法,通用参数通常由编译器推断。 In certain situations you might want to specify the generic arguments yourself, using a slightly peculiar syntax: 在某些情况下,您可能希望使用稍微特殊的语法自己指定泛型参数:

    List<String> strings = Collections.<String>emptyList();

In this case, the compiler could have inferred the type, but it's not always obvious whether the compiler can or can't. 在这种情况下,编译器可以推断出类型,但编译器是否可以并不总是很明显。 Note, the <> is after the dot. 注意, <>在点之后。 For syntactical reasons the type name or target object must always be specified. 出于语法原因,必须始终指定类型名称或目标对象。

It's possible to have generic constructors, but I've never seen one in the wild and the syntax gets worse. 有可能有通用的构造函数,但我从来没有见过一个,语法变得更糟。

I believe C++ and C# syntaxes place the generic types after the method/function name. 我相信C ++和C#语法将泛型类型放在方法/函数名之后。

The context is a generic method as opposed to a class. 上下文是一个泛型方法而不是类。 The variable <T> applies only to the call of the method.. The Collections class has a number of these; 变量<T>仅适用于方法的调用.Collections类有许多这些; the class itself is not generic, but many of the methods are. 类本身不是通用的,但许多方法都是。

The compiler decides what T is equal to -- it equals whatever gets the types to work. 编译器决定T等于什么 - 它等于获得类型的工作。 Sometimes this is easier then others. 有时这比其他人容易。

For example, the method static <T> Set<T> Collections.singleton(T o) the type is defined in the parameter: 例如,方法static <T> Set<T> Collections.singleton(T o)的类型在参数中定义:

Collections.singleton(String T)

will return a Set<String> . 将返回Set<String>

Sometimes the type is hard to define. 有时类型很难定义。 For example sometimes there is not easily enough information to type Collection.emptyList() . 例如,有时没有足够的信息来输入Collection.emptyList() In that case you can specify the type directly: Collection.<String>emptyList() . 在这种情况下,您可以直接指定类型: Collection.<String>emptyList()

T it's the formal type parameter wich will be replaced by the actual type argument used at the instantiation of the object. 这是正式的类型参数,它将被在对象实例化时使用的实际类型参数替换。

For example, here is the List and Iterator definitios in package java.util: 例如,这是包java.util中的List和Iterator definitios:

public interface List<E>{
  void add(E x);
  Iterator<E> iterator();
}

public interface Iterator<E>{
  E next();
  boolean hasNext();
}

Then you can instantiate a List this way: 然后你可以这样实例化List:

List<String> ls = new ArrayList<String>()

Where you might imagine that List stands for a version of List where E has been uniformly replaced by String: 您可能会想到List代表List的一个版本,其中E已被String统一替换:

public interface StringList{
  void add(String x)
  Iterator<String> iterator();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM