什么是< X>在java方法声明中意味着什么? [英] What does <X> mean in java method declarations?
问题描述
我正在浏览 EntityManager.java
,我看到
public< T extends Object> T find(Class< T> type,Object o,LockModeType lmt,Map< String,Object> map);
我从未见过< X>
在类方法定义中键入声明。这是什么意思?
public< T extends Object> T find(
)好像它定义了T将扩展另一个类。我是否理解这需要你扩展另一个对象?
public< T extends Number> T find()
这意味着您可以定义一个通用函数,它具有一个用T表示的类型参数,T必须是Number的一个子类(extends),该函数返回一个类型为T的对象。
然后你可以调用这样的函数: Double d = find();
Integer i = find();
但是以下是编译时错误:
String s = find(); //错误
因为字符串不会扩展数字。
I'm looking through EntityManager.java
and I see
public <T extends Object> T find(Class<T> type, Object o, LockModeType lmt, Map<String, Object> map);
I've never seen <X>
type declarations within a class method definition. What does this mean?
public <T extends Object> T find(
seems like it's defining that T will extend another class. Am I understanding that this requires you to extend another object?
public <T extends Number> T find()
This means that you define a generic function, which has a type parameter denoted by T for the sake of definition. T has to be a subclass (extends) of Number. The function returns an object of type T.
Then you can call the function like that:
Double d = find();
Integer i = find();
but you the following is a compile time error:
String s = find(); //error
because String doesn't extend Number.
这篇关于什么是< X>在java方法声明中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!