简体   繁体   English

什么课 <? extends ParentClass> 意思?

[英]What does Class<? extends ParentClass> mean?

I found a method like the one below. 我发现了一种类似于下面的方法。

public void simpleTest(Class <? extends ParentClass> myClass){

}

I didn't understand the expression : Class <? extends ParentClass> myClass 我不明白表达: Class <? extends ParentClass> myClass Class <? extends ParentClass> myClass here. 在此处Class <? extends ParentClass> myClass

Can anyone explain it? 有人可以解释吗?

Class <? extends ParentClass> myClass Class <? extends ParentClass> myClass is a method argument whose type is a Class that is parameterized to ensure that what's passed is a Class that represents some type that is a subtype of ParentClass. Class <? extends ParentClass> myClass是一个方法参数,其类型是Class ,它被参数化以确保传递的是一个Class,它表示某种类型,该类型是ParentClass的子类型

ie given: 即给出:

class ParentParentClass {}
class ParentClass extends ParentParentClass {}
class ChildClass extends ParentClass {}
class ChildChildClass extends ChildClass {}

public void simpleTest(Class <? extends ParentClass> myClass) {}

These are valid: 这些是有效的:

simpleTest(ParentClass.class);
simpleTest(ChildClass.class);
simpleTest(ChildChildClass.class);

These aren't valid because the argument doesn't "fit" inside the required type: 这些无效,因为参数不能“适合”所需的类型:

simpleTest(ParentParentClass.class);
simpleTest(String.class);
simpleTest(Date.class);
simpleTest(Object.class);

From the javadoc of java.lang.Class<T> : java.lang.Class<T>的javadoc中:

T - the type of the class modeled by this Class object. T-此Class对象建模的类的类型。 For example, the type of String.class is Class<String> . 例如,String.class的类型为Class<String> Use Class<?> if the class being modeled is unknown. 如果要建模的类未知,请使用Class<?>

Now replace String with your class: it's a Class of a type which is a ParentClass or a subclass of ParentClass. 现在,将String替换为您的类:它是类型为Class的Class ,它是ParentClass或ParentClass的子类。

Class myClass - means that myClass should be aa sub type of ParentClass. 类myClass-表示myClass应该是ParentClass的子类型。

class MyClass extends ParentClass {
}

simpleTest(ParentClass.class); // this is ok
simpleTest(MyClass.class); // this is ok

class SomeOtherClass {
}
simpleTest(SomeOtherClass.class); // will result in compiler error

It's a generic method taking a parametrized class. 这是采用参数化类的通用方法。

It might be helpful to first consider a simpler generics example: List<? extends ParentClass> 首先考虑一个更简单的泛型示例可能会有所帮助: List<? extends ParentClass> List<? extends ParentClass> means it's a List that only takes objects that fulfill the is-a relationship with ParentClass. List<? extends ParentClass>意味着它是一个List,仅接受与ParentClass满足is-a关系的对象。 Ie the parameter of the List implementation is-a ParentClass, but the type of the whole thing List<? extends ParentClass> 即List实现的参数是-ParentClass,但是整件事的类型List<? extends ParentClass> List<? extends ParentClass> is List. List<? extends ParentClass>是List。

In your example, just substitute "Class" instead of "List". 在您的示例中,仅用“类”代替“列表”。 myClass is a Class, ie you can call the method with something like "MyClassName.class". myClass是一个类,即您可以使用“ MyClassName.class”之类的方法来调用该方法。 Further, the parameter of the Class of my Class is-a ParentClass. 此外,我的班级的班级参数是-ParentClass。 This basically means that you can only pass the Class of ParentClass or its subclasses to the method. 这基本上意味着您只能将ParentClass的类或其子类传递给方法。

See also: http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html 另请参阅: http : //docs.oracle.com/javase/7/docs/api/java/lang/Class.html

暂无
暂无

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

相关问题 这是什么意思List &lt;Class <? extends DataType> &gt;? - What does this mean List < Class <? extends DataType>>? 这个 Java 语法是什么意思? (`类<? extends E>克拉兹`) - What does this Java syntax mean? (` Class<? extends E> clazz`) 在 Java 的泛型类中,“T 扩展垃圾”是什么意思? - What does "T extends Junk" mean in a generic class in Java? 这个Java语法是什么意思? (`类 <? extends ContactAccessor> clazz`) - What does this Java syntax mean? (`Class<? extends ContactAccessor> clazz`) “扩展JPanel”是什么意思? - What does “extends JPanel” mean? 做什么<t extends mean?< div><div id="text_translate"><p> 我见过如下所示的方法:</p><pre> protected &lt;T extends ABC&gt; T save( T Acd, boolean en) {</pre><p> 它有什么作用? 这些类型的方法声明在 Java 中称为什么?</p></div></t> - What does <T extends mean? 复杂泛型类型声明的解释。 什么类 <? extends Class<? extends Actor> []&gt;实际意味着什么? - Explanation for convoluted generic-type declaration. What does Class<? extends Class<? extends Actor>[]> actually mean? 那是什么? 表示“格式”(列表 <? extends NameValuePair> ” - What does the ? mean in “format(List<? extends NameValuePair>” 列出什么 <JAXBElement<? extends SomeClassName> &gt;是什么意思? - What does List<JAXBElement<? extends SomeClassName>> mean? “ @Override”和“ <? extends Page> “ 意思? - What does “@Override” and “<? extends Page>” mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM