Java中的getClass方法 [英] getClass method in java
问题描述
Object类中的getClass方法如何能够动态返回Class?
How is it that the getClass method in Object class is capable of dynamically returning Class?
推荐答案
它不返回类 name -它返回表示该对象类型的 Class
.每个对象都知道"它实际上是什么类型-这取决于执行时间类型,即强制转换如何工作或失败. Object.getClass()
只是从对象中检索相关信息.
It doesn't return the class name - it returns the Class
representing that object's type. Each object "knows" what type it really is - that's how casts can work or fail depending on the execution-time type. Object.getClass()
just retrieves the relevant information from the object.
如果您有一个仅包含单个 int
的类,则每个对象在内存中仍将占用4个以上的字节:实际上是一个对象标头",其中包含对对象的引用等内容实际类型,与该对象关联的监视器有关的信息等.
If you have a class which only contains a single int
, each object will still take up more than 4 bytes in memory: there's effectively an object "header" containing things like a reference to the object's actual type, information to do with the monitor associated with that object etc.
这篇关于Java中的getClass方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!