import java.util.HashMap; import java.util.Map; public class MapTest { public static void main(String[] args) { System.out.println(HashMap.class.isAssignableFrom(Map.class)); System.out.println(Map.class.isAssignableFrom(HashMap.class)); } }
如上代码返回:
false true
isAssignable方法的注释
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 specified Class parameter.
从上面这行可以看出,如果与参数类型相同,或者是参数类型的父类,都返回true