简体   繁体   English

如何获取实例编号

[英]How to get instance number

Is there way how to get instance number or id, I mean that what you can see in eclipse Debug>Variables window after class name in value column. 有没有办法获取实例编号或ID,我的意思是你可以在值列中的类名后面的eclipse Debug> Variables窗口中看到。

Thanks 谢谢

See System.identityHashCode which returns the number that would be returned if Object.hashCode had not been overridden on an object. 请参阅System.identityHashCode ,它返回在对象上未覆盖Object.hashCode将返回的数字。 It is not a perfect proxy for identity since multiple objects might have the same identity hash code, but it is pretty useful for debugging. 它不是完美的身份代理,因为多个对象可能具有相同的身份哈希码,但它对于调试非常有用。

By default, debugger shows you the result of toString() method with the hashCode of the object. 默认情况下,调试器使用对象的hashCode显示toString()方法的结果。 So, you can see the value by executing hashCode() method. 因此,您可以通过执行hashCode()方法来查看该值。

我想你是在引用Objects toString()实现:

return getClass().getName() + "@" + Integer.toHexString(hashCode());

Maybe I'm wrong, but I don't think instances of a class are numbered. 也许我错了,但我不认为班级的实例有数字。 I have searched many times for a way to get instances of a particular class and according to my searches, it would be impossible. 我已多次搜索获取特定类的实例的方法,根据我的搜索,这是不可能的。 Instances have an address, but I see no use for a class instance number (objects are not sorted by class in memory AFAIK). 实例有一个地址,但我认为没有用于类实例编号(对象不按内存AFAIK中的类排序)。 Note that you can always create a mechanism for giving numbers to you instances of a particular class. 请注意,您始终可以创建一种机制,为特定类的实例提供数字。 Have a static count in the class and give its value to each new instance you create (in the constructor) and increment it each time. 在类中有一个静态计数,并将其值赋给您创建的每个新实例(在构造函数中)并每次递增它。 That would be a way to differentiate instances. 这将是区分实例的一种方法。

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

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