Java泛型2套< K,V>或< T>在方法声明中 [英] Java generics 2 set of <K,V> or <T> in method declaration

查看:84
本文介绍了Java泛型2套< K,V>或< T>在方法声明中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看下面的方法声明,但我不明白< K,V> < T> 正在被使用。



在这个方法中做什么 public < / code>

  public< K,V> void add(K k,V v)

static ; 在这个方法中做了什么?

  public static< T> int countGreaterThan(T [] anArray,T elem)


解决方案

回想一下,在Java中,所有的方法都必须在一个类中,所以这个方法将在某种类中,可以说是Clazz。

public 表示这个方法可以从类之外的任何地方被任何人访问。

static code>表示当你调用这个方法时,你不需要从一个特定的对象调用它,而是你可以使用类名来调用它。因此,您可以通过输入 Clazz.countGreaterThan 来调用countGreaterThen,而不是使用创建为 Clazz


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套&lt; K,V&gt;或&lt; T&gt;在方法声明中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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