java – Object. Signature of getclass() method
•
Java
The object class contains the following methods:
public final Class<? extends Object> getClass().
Why is the return type of this method class
Solution
Because Java lacks self types (if it has them, the return type will be class ) So the signature just declares class , It can do the best (next), which is not ideal - the compiler must know that getClass () does not return any class, but a class, which is the static type of an expression whose subtype calls getClass ()
Therefore, we have this singularity, where the signature declaration of the method is class Due to language constraints, the compiler treats the return value as class because it knows it's safe and wants to help:)
PS: have you seen it? Extends Object > because you called getClass () in an expression whose static type is object
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码