Why does getClass return a Class<? extends |X|>?

后端 未结 1 658
臣服心动
臣服心动 2021-01-18 23:07

I tried to understand what is the reason for getClass method to return Class?

From the openjdk near public final

相关标签:
1条回答
  • 2021-01-18 23:33
    Foo foo = new SubFoo();
    

    What do you expect foo.getClass() to return? (It's going to return SubFoo.class.)

    That's part of the whole point: that getClass() returns the class of the real object, not the reference type. Otherwise, you could just write the reference type, and foo.getClass() would never be any different from Foo.class, so you'd just write the second one anyway.

    (Note, by the way, that getClass() actually has its own special treatment in the type system, not like any other method, because SubFoo.getClass() does not return a subtype of Foo.getClass().)

    0 讨论(0)
提交回复
热议问题