在 Java 中, Class
有一个 isAssignableFrom
method定义如下:
public boolean isAssignableFrom(Class<?> cls)
Determines if the class or interface represented by this
Class
object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specifiedClass
parameter. It returnstrue
if so; otherwise it returnsfalse
. If thisClass
object represents a primitive type, this method returnstrue
if the specifiedClass
parameter is exactly thisClass
object; otherwise it returnsfalse
.Specifically, this method tests whether the type represented by the specified
Class
parameter can be converted to the type represented by thisClass
object via an identity conversion or via a widening reference conversion. See The Java Language Specification, sections 5.1.1 and 5.1.4 , for details.Parameters:
cls
- theClass
object to be checkedReturns:
the
boolean
value indicating whether objects of the typecls
can be assigned to objects of this class
Class
实现 Type
界面。有没有等价的 isAssignableFrom
适用于 Type
的方法s 而不仅仅是 Class
是吗?例如,是否有一种方法可以确定类型为 List<String>
的变量是否存在? (将通过 ParameterizedType
的实例表示)可以分配给 List<? extends Object>
类型的变量?
最佳答案
看看javaRuntype -- 我发现它很有用。它可以根据 java.lang.reflect.Type
的实例生成类型表示,并测试它们之间的可分配性。
关于java - 是否有 Class.isAssignableFrom 与 Type 对象一起使用的替代方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7920023/