Java泛型2套< K,V>或< T>在方法声明中 [英] Java generics 2 set of <K,V> or <T> in method declaration
问题描述
我一直在看下面的方法声明,但我不明白< K,V>
和< T>
正在被使用。
在这个方法中做什么 public
public< K,V> void add(K k,V v)
public static< T> int countGreaterThan(T [] anArray,T elem)
回想一下,在Java中,所有的方法都必须在一个类中,所以这个方法将在某种类中,可以说是Clazz。
public
表示这个方法可以从类之外的任何地方被任何人访问。
static code>表示当你调用这个方法时,你不需要从一个特定的对象调用它,而是你可以使用类名来调用它。因此,您可以通过输入
Clazz.countGreaterThan
来调用countGreaterThen,而不是使用创建为
I have been seeing the following methods declaration but I do not understand how <K, V>
and <T>
are being used.
What does public <K, V>
does in this method?
public <K, V> void add(K k, V v)
What does static <T>
does in this method?
public static <T> int countGreaterThan(T[] anArray, T elem)
Recall, that in Java, all methods MUST be inside a class, so this method will be in some sort of class, lets say "Clazz".
public
indicates that this method can be accessed from outside the class, by absolutely anyone.
static
indicated that when you call this method, you don't need to call it from a specific object, but rather you can call it just by using the class name. So you would be able to call countGreaterThen by typing Clazz.countGreaterThan
instead of using a specific object created as an instance of Clazz
这篇关于Java泛型2套< K,V>或< T>在方法声明中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!