Java反射,对象有什么类?
我正在尝试对小程序进行一些反思。 我发现的是一些整数、字符串、对象等数组。
例如,如果有一个带有 object[] 和 object[0].toString() = [I@7593c366 然后我知道它是一个整数数组。但如果它说 aa@98324ca33 is it’s class then aa 呢? 我使用类加载器,所以当我看到这个时,我的第一个猜测是我需要加载aa类(@之前的部分,并使用其中的对象。但我不确定@之前的部分是类。有人可以说我吗?这是正确的吗?或者有其他想法吗
?
im trying to do some reflection on a applet.
things i found are some arrays of ints, strings, objects etc.
for example, if there was a field with an object[] and object[0].toString() = [I@7593c366
then i know its an integer array. but what if it says aa@98324ca33 is it's class then aa?
im using a classloader, so my first guess when i see this i need to load the aa class (part before the @, and use the object in it. but im not sure the part befor the @ is the class. can somebody say me this is right? or got other ideas?
thnx!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应该为此使用
toString()
- 一方面,它可能已被覆盖。举一个简单的例子:如果您认为
x
在这里指的是aa
类型的对象,那显然是错误的 - 它指的是一个字符串。您只需调用
getClass()
即可找出任何对象的类:目前并不清楚您要做什么或从哪里获取信息,但是您绝对不应该使用
toString
来确定所涉及的类。You shouldn't use
toString()
for this - for one thing, it can have been overridden. As a straightforward example:You would clearly be wrong to think that
x
refers to an object of typeaa
here - it refers to a string.You can find out the class of any object just by calling
getClass()
on it:It's not really clear what you're trying to do or where you're getting information from in the first place, but you definitely shouldn't be using
toString
to determine the class involved.是的,@之前的部分是类 fqn,但你不应该依赖它。对象可以重写
toString()
,然后你的逻辑就会失败。请改用 obj.getClass() 。
Yes, the part before @ is the class fqn, but you should not rely on that. Object can override
toString()
and then your logic will fail.Use
obj.getClass()
instead.看一下
java.lang 类。类
。只需在对象上调用getClass
即可检索其类,而不是使用toString
方法。如果要检查它是否是数组,可以使用
Class#isArray()
方法反过来也是可能的。如果您有一个 Class 实例,则可以使用
Class#isInstance( Object )
方法Take a look at the class
java.lang.Class
. Just callgetClass
on an object to retrieve its class instead of using thetoString
methodIf you want to check whether it is an array, you can use to
Class#isArray()
methodThe other way around is also possible. If you have a
Class
instance, you can determine whether an object belongs to this class by using theClass#isInstance( Object )
method