Java反射,对象有什么类?

发布于 2024-12-25 16:09:40 字数 266 浏览 1 评论 0原文

我正在尝试对小程序进行一些反思。 我发现的是一些整数、字符串、对象等数组。

例如,如果有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Oo萌小芽oO 2025-01-01 16:09:40

您不应该为此使用 toString() - 一方面,它可能已被覆盖。举一个简单的例子:

Object x = "aa@98324ca33";
String bogusClassName = x.toString();

如果您认为 x 在这里指的是 aa 类型的对象,那显然是错误的 - 它指的是一个字符串。

您只需调用 getClass() 即可找出任何对象的类:

Object x = new SomeType();
Class<?> clazz = x.getClass();

目前并不清楚您要做什么或从哪里获取信息,但是您绝对不应该使用toString来确定所涉及的类。

You shouldn't use toString() for this - for one thing, it can have been overridden. As a straightforward example:

Object x = "aa@98324ca33";
String bogusClassName = x.toString();

You would clearly be wrong to think that x refers to an object of type aa here - it refers to a string.

You can find out the class of any object just by calling getClass() on it:

Object x = new SomeType();
Class<?> clazz = x.getClass();

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.

酒儿 2025-01-01 16:09:40

是的,@之前的部分是类 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.

桜花祭 2025-01-01 16:09:40

看一下 java.lang 类。类。只需在对象上调用 getClass 即可检索其类,而不是使用 toString 方法。

Object anObject = ... ;
Class<?> clazz = anObject.getClass();

如果要检查它是否是数组,可以使用 Class#isArray() 方法

clazz.isArray()

反过来也是可能的。如果您有一个 Class 实例,则可以使用 Class#isInstance( Object ) 方法

clazz.isInstance( anObject );

Take a look at the class java.lang.Class. Just call getClass on an object to retrieve its class instead of using the toString method

Object anObject = ... ;
Class<?> clazz = anObject.getClass();

If you want to check whether it is an array, you can use to Class#isArray() method

clazz.isArray()

The other way around is also possible. If you have a Class instance, you can determine whether an object belongs to this class by using the Class#isInstance( Object ) method

clazz.isInstance( anObject );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文