简体   繁体   English

关于Class.isInstance的困惑

[英]confusion about Class.isInstance

Is it possible in java to get the complete hierarchy of an object? 在Java中能否获得对象的完整层次结构? I have checked the javadoc for the Class class and i cant find such a method. 我已经检查了javadoc的Class类,但是我找不到这种方法。 Also, what i find strange is the result for the following code: 另外,我发现奇怪的是以下代码的结果:

public static void main(String[] args) throws ClassNotFoundException {
    ClassLoader loader = ClassLoaderTest.class.getClassLoader();
    Class<?> clazz = loader.loadClass("java.lang.Integer");
    System.out.println(clazz.getSuperclass()); //prints java.lang.Number
    System.out.println(clazz.isInstance(clazz.getSuperclass())); //returns false??

    Number number = new Integer(1);//no class cast exception
}

The doc for the isInstance() method says: isInstance()方法的文档说:

Determines if the specified {@code Object} is assignment-compatible * with the object represented by this {@code Class}. 确定指定的{@code对象}与该{@code类}表示的对象是否兼容分配*。

So since we can assign Integer to Number, why does: 因此,由于我们可以将整数分配给Number,为什么这样做:

java.lang.Integer.class.isInstance(java.lang.Number.class);

return false? 返回假?

Thx 谢谢

To re-quote the javadoc 重新引用javadoc

Determines if the specified Object is assignment-compatible * with the object represented by this Class . 确定指定Object是赋值兼容*与由此表示的对象Class

In the expression 在表达中

java.lang.Integer.class.isInstance(java.lang.Number.class);

you are checking if the object returned by the expression java.lang.Number.class is an instance of Integer . 您正在检查表达式java.lang.Number.class返回的对象是否为Integer的实例。 It is not, it is an instance of java.lang.Class . 不是,它是java.lang.Class的实例。

It should be used like 它应该像

java.lang.Integer.class.isInstance(new Integer(1)); // if you want it to return true

You can pass it anything you want, but it will only return true if the argument used is an Integer or one of its sub types (but it is final so there aren't any). 您可以根据需要传递任何值,但仅当使用的参数是Integer或其子类型之一(但它是final所以没有任何值)时,它才返回true

Integer is a sub type of Number . IntegerNumber的子类型。

Any Integer instance can be used where a Number object is required. 在需要Number对象的情况下,可以使用任何Integer实例。

In order to obtain the "complete hierarchy", you can recursively walk from the starting class through all its superlcasses and their implemented interfaces 为了获得“完整的层次结构”,您可以递归地从起始类中遍历所有父类及其实现的接口。

import java.util.LinkedHashSet;
import java.util.Set;

public class Superclasses
{
    public static void main(String[] args)
    {
        Class<?> c = Integer.class;

        System.out.println("Superclasses:");
        Set<Class<?>> superclasses = superclasses(c);
        for (Class<?> s : superclasses)
        {
            System.out.println(s);
        }
    }

    private static Set<Class<?>> superclasses(Class<?> c)
    {
        LinkedHashSet<Class<?>> set = new LinkedHashSet<Class<?>>();
        superclasses(c, set);
        return set;
    }

    private static void superclasses(Class<?> c, Set<Class<?>> set)
    {
        if (c == null)
        {
            return;
        }
        set.add(c);
        if (c.equals(Object.class))
        {
            return;
        }
        superclasses(c.getSuperclass(), set);
        for (Class<?> i : c.getInterfaces())
        {
            superclasses(i, set);
        }
    }

}

Note that the situation is slightly more complicated when you als want to obtain the generic superclasses. 请注意,当您要获取泛型超类时,情况会稍微复杂一些。 For example, when you have a class like List<Integer> and want to obtain all superclasses including Collection<? extends Number> 例如,当您拥有类似List<Integer>的类并想要获取包括Collection<? extends Number>所有超类时Collection<? extends Number> Collection<? extends Number> and Iterable<? extends Serializable> Collection<? extends Number>Iterable<? extends Serializable> Iterable<? extends Serializable> . Iterable<? extends Serializable> I have implemented this, but it is a bit of a hassle, because the set of superclasses in this sense is infinite, and you have to stop somewhere... 我已经实现了这一点,但这有点麻烦,因为从这个意义上说,超类的集合是无限的,您必须停在某个地方...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM