简体   繁体   English

Java 反射得到超类得到了意想不到的结果

[英]Java reflection gets superclass got unexpected result

While learning java reflection, I got some problem when testing getSuperclass()在学习 java 反射时,我在测试getSuperclass()时遇到了一些问题

Below is my test code:下面是我的测试代码:

System.out.println(java.io.Closeable.class.getSuperclass());

In my expectation, this line shall print something like AutoClosable because this is the class which Closable extends from.在我的预期中,这一行应该打印出类似AutoClosable的内容,因为这是Closable扩展的 class。 However this turns out to be null .然而,事实证明这是null I tried to explain this as Closable locates in java.io while AutoClosable locates in java.lang and getSuperclass() would only search in the package scope. I tried to explain this as Closable locates in java.io while AutoClosable locates in java.lang and getSuperclass() would only search in the package scope.

But this explanation does not make sense if I write like this:但是如果我这样写,这种解释就没有意义了:

Closeable closeable = () -> {

    };
System.out.println(closeable.getClass().getSuperclass());

The code above results in class java.lang.Object .上面的代码导致class java.lang.Object The result makes this problem more peculiar and hard to understand.结果使这个问题更加奇特且难以理解。

How does java implements this getSuperclass() method and why does not this method return the result which corresponds to its name? java 是如何实现这个getSuperclass()方法的,为什么这个方法没有返回与其名称对应的结果?

Neither Closeable nor AutoCloseable are classes, they are both interfaces. CloseableAutoCloseable都不是类,它们都是接口。

getSuperclass only returns super-classes, not super-interfaces. getSuperclass只返回超类,而不是超接口。 If called on something that is not a class itself (such as an interface, or a primitive type), the method returns null .如果调用不是 class 本身的东西(例如接口或原始类型),则该方法返回null

You may want to call getInterfaces instead.您可能想改为调用getInterfaces

Closeable is an interface, it has no superclass, only superinterfaces. Closeable是一个接口,它没有超类,只有超接口。

As it says in the documentation of getSuperclass() :正如它getSuperclass()的文档中所说:

If this Class represents either the Object class, an interface, a primitive type, or void, then null is returned.如果此 Class 表示 Object class、接口、原始类型或 void,则返回 Z37A6259CC64C1DAE299D8。

Closeable closeable = () -> {

};

is effectively creating concrete class which implements Closeable : Closeable has a single abstract method, close() , which you are implementing with an empty body.正在有效地创建实现CloseableCloseable有一个抽象方法close() ,您正在用一个空主体来实现它。 This is effectively the same as writing:这实际上与编写相同:

Closeable closeable = new Closeable() {
  @Override public void close() {}
}

This class does have a superclass, because all classes except Object have a superclass.这个 class确实有一个超类,因为除了Object之外的所有类都有一个超类。 That superclass is Object , per the spec : 根据规范,该超类是Object

If the Identifier in ClassOrInterfaceTypeToInstantiate denotes an interface, I , then an anonymous direct subclass of Object that implements I is declared.如果ClassOrInterfaceTypeToInstantiate中的Identifier表示接口I ,则声明实现IObject的匿名直接子类。

That method getSuperClass says:该方法getSuperClass说:

Returns the Class representing the superclass of the entity (class, interface, primitive type or void) represented by this Class.返回表示由此 Class 表示的实体(类、接口、原始类型或 void)的超类的 Class。

In your case, the super class is Object.在您的情况下,超级class是 Object。 Because you are looking at a class instance.因为您正在查看 class 实例。

Meaning: in your second example, you created a lambda, basically: an anonymous inner class .含义:在第二个示例中,您创建了一个 lambda,基本上:一个匿名内部class

To gather information about implemented (or extended) interfaces, use getInterfaces .要收集有关已实现(或扩展)接口的信息,请使用getInterfaces

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

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