[英]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>
。 UseClass<?>
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.