java instanceof 和 getClass() 未按预期工作

标签 java inheritance

我有以下代码:

protected LogEvent toLogEvent(LogRecord element) {
    ......
    System.out.println("element.getClass():"+element.getClass());
    System.out.println("element.getClass() == org.jboss.logmanager.ExtLogRecord.class:" + (element.getClass() == org.jboss.logmanager.ExtLogRecord.class));
    System.out.println("element instanceof org.jboss.logmanager.ExtLogRecord:" + (element instanceof org.jboss.logmanager.ExtLogRecord));
    System.out.println("element instanceof java.util.logging.LogRecord:"+(element instanceof java.util.logging.LogRecord));
    System.out.println("element.toString():"+element.toString());

输出为:

09:20:51,544 INFO  stdout element.getClass():class org.jboss.logmanager.ExtLogRecord
09:20:51,545 INFO  stdout element.getClass() ==org.jboss.logmanager.ExtLogRecord.class:false
09:20:51,547 INFO  stdout element instanceof org.jboss.logmanager.ExtLogRecord:false
09:20:51,548 INFO  stdout element instanceof java.util.logging.LogRecord:true
09:20:51,549 INFO  stdout element.toString():org.jboss.logmanager.ExtLogRecord@2657c0ad

ExtLogRecordLogRecord 的子类。

我的问题是:

  1. 第二行为什么输出为false?显然,元素是 ExtLogRecord 的一个实例。

  2. 第三行元素 instanceof ExtLogRecord 返回 false,第四行元素 instanceof LogRecord 返回 true。所以元素是一个LogRecord,但它不是一个ExtLogRecord。这是怎么发生的?

更新:

我添加了一些有关类加载的附加代码。输出为:

10:32:48,372 INFO  stdout (new ExtLogRecord()) instanceof  ExtLogRecord:true
10:32:48,374 INFO  stdout new ExtLogRecord(org.jboss.logmanager.Level.ALL,"","").getClass():class org.jboss.logmanager.ExtLogRecord
10:32:48,376 INFO  stdout LogRecord.class.getClassLoader():null
10:32:48,377 INFO  stdout ExtLogRecord.class.getClassLoader():"ModuleClassLoader for Module "org.tamin.tiba.logging.jboss.handlers:main" from local module loader@416a8198(roots: D:\java\jbossas\modules)
10:32:48,378 INFO  stdout LogRecord.class.getClassLoader() == ExtLogRecord.class.getClassLoader():false

更新2:

其他类加载比较:

10:43:14,113 INFO  stdout element.getClass().getClassLoader() == ExtLogRecord.class.getClassLoader():false
10:43:14,115 INFO  stdout element.getClass().getClassLoader():ModuleClassLoader for Module "org.jboss.logmanager:main" from local module loader @576fb9a5 (roots: D:\java\jbossas\modules)

最佳答案

当您传递 LogRecord 实例而不是子类实例时,您将获得这些输出。考虑这两个类,并假设它们有一个 0-arg 构造函数,下面是使用 2 个实例调用该方法的结果:

toLogEvent(new LogRecord());     // passing superclass instance
System.out.println();
toLogEvent(new ExtLogRecord());  // passing subclass instance

输出:

element.getClass():class LogRecord
element.getClass() == ExtLogRecord.class:false
element instanceof ExtLogRecord:false
element instanceof LogRecord:true
element.toString():LogRecord@182f0db

element.getClass():class ExtLogRecord
element.getClass() == ExtLogRecord.class:true  
element instanceof ExtLogRecord:true  
element instanceof LogRecord:true
element.toString():ExtLogRecord@192d342

输出是显而易见的。父类(super class)的实例不是也不可能是其任何子类的实例。同样,LogRecord.class 不能等于 ExtLogRecord.class

关于java instanceof 和 getClass() 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20209885/

相关文章:

java - 编写一个循环 1 到 100 因数的程序。当它达到有九个因数的数字时应该停止

即使调用cancel方法后,Java线程也不会停止?

c++ - 错误 : out-of-line definition of 'test' does not match any declaration in 'B<dim>'

python - 在类范围内动态覆盖列表的内置函数

java - 从递归删除Java中获取对象

java - 使用 Spring WebClient 时没有从端点接收到数据,但我可以使用curl 来获取数据

java - JXList 排序不起作用(swingx 库)

C++通过指针更新派生类对象中的变量

c++ - 为什么 x 成员变量不明确?

c++ - 如何从头文件中访问类的私有(private)成员?