[英]What does this Java syntax mean? (` Class<? extends E> clazz`)
I need a custom method to check for a list containing an instance of a class and call this method but I do not understand this syntax "Class clazz" and I do not understand What's the second parameter of this method我需要一个自定义方法来检查包含类实例的列表并调用此方法,但我不理解此语法“Class clazz”,我不明白此方法的第二个参数是什么
public static <E> boolean containsInstanceOfOidInrArraylist(List<E> Arraylist, Class<? extends E> clazz) {
for (E e : Arraylist) {
if (clazz.isInstance(e)) {
return true;
}
}
return false;
}
Ok, so the first parameter of the function is a List<E>
named Arraylist
(You shouldn't capitalize variables in Java, name it as such arrayList
).好的,所以该函数的第一个参数是一个名为
Arraylist
的List<E>
(您不应该在 Java arrayList
变量大写,将其命名为arrayList
)。
The second parameter is a Class<? extends E>
第二个参数是一个
Class<? extends E>
Class<? extends E>
named clazz
. Class<? extends E>
命名为clazz
。
Check manub's explaination of Class<?>
:检查manub对
Class<?>
的解释:
Class
is a parameterizable class, hence you can use the syntaxClass<T>
whereT
is a type.Class
是一个可参数化的类,因此您可以使用语法Class<T>
其中T
是一种类型。 By writingClass<?>
, you're declaring aClass
object which can be of any type (?
is a wildcard).通过编写
Class<?>
,您声明了一个可以是任何类型的Class
对象(?
是通配符)。 TheClass
type is a type that contains meta-information about a class.Class
类型是一种包含有关类的元信息的类型。
So now you know what Class<?>
means, but what about Class<? extends E>
所以现在你知道
Class<?>
是什么意思了,但是Class<? extends E>
Class<? extends E>
? Class<? extends E>
?
<? extends E>
<? extends E>
basically means any class which extends E (or E itself). <? extends E>
基本上意味着任何扩展 E(或 E 本身)的类。
So Class<? extends E> clazz
所以
Class<? extends E> clazz
Class<? extends E> clazz
means you have a varaible named clazz
which is E
class or a sub class of E
. Class<? extends E> clazz
意味着你有一个名为varaible clazz
是E
类或子类的E
。
See this tutorial: https://docs.oracle.com/javase/tutorial/java/generics/upperBounded.html请参阅本教程: https : //docs.oracle.com/javase/tutorial/java/generics/upperBounded.html
"Upper Bounded Wildcards "上界通配符
You can use an upper bounded wildcard to relax the restrictions on a variable.您可以使用上限通配符来放宽对变量的限制。 For example, say you want to write a method that works on
List<Integer>
, List<Double>
, and List<Number>
;例如,假设您想编写一个适用于
List<Integer>
、 List<Double>
和List<Number>
; you can achieve this by using an upper bounded wildcard.您可以通过使用上限通配符来实现这一点。
To declare an upper-bounded wildcard, use the wildcard character ('?'), followed by the extends keyword, followed by its upper bound.要声明上限通配符,请使用通配符 ('?'),后跟 extends 关键字,然后是其上限。 Note that, in this context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces)."
请注意,在此上下文中,extends 在一般意义上用于表示“扩展”(如在类中)或“实现”(如在接口中)。
The second parameter is a class that is the same as the class of the objects in the list, or extends it.第二个参数是一个与列表中对象的类相同的类,或者扩展它。 clazz is used as a variable name because class is a reserved keyword and cannot be used.
clazz 用作变量名,因为 class 是保留关键字,不能使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.