Java有检查对象类型的方法吗?

发布于 2024-12-02 15:56:15 字数 179 浏览 4 评论 0原文

在 Javascript 中,如果你想检查 chrome 中的某个对象,

console.log(object) 会打印出该对象的变量和方法。

Ruby 还具有 object.inspect,它返回有关对象的基本信息。

我应该在 Java 中使用什么代码来检查对象?

In Javascript, if you want to inspect some object in chrome,

console.log(object) will printout variables, and methods about the object.

Ruby also has object.inspect which returns basic information about the object.

What code should I use in Java to inspect an object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

笙痞 2024-12-09 15:56:15

检查 Java 对象所需的所有代码都位于 java.lang. lang.reflect 包。

不过,您必须使用该 API 自己编写大量代码。

Apache 的 BeanUtils 稍微简单一些。

All the code you need to inspect Java objects is in the java.lang.reflect package.

You'll have to write a fair bit of code yourself using that API though.

Apache's BeanUtils is somewhat easier.

窝囊感情。 2024-12-09 15:56:15

您想要使用 Java 反射 API

例如,试试这个:

Class c = object.getClass();
System.out.writeln("Looks like you have a "+c.getCanonicalName());

You want to use the Java Reflection API.

For example, try this:

Class c = object.getClass();
System.out.writeln("Looks like you have a "+c.getCanonicalName());
爱冒险 2024-12-09 15:56:15

如果您在代码运行时需要它,则 提供类似 console.log 的内容log4j。您将记录的信息可以通过 Java Reflection

检索用于转储Java 对象,请查看此处的答案,了解各种实现方法。我会使用 XStream 因为它易于使用到期。

如果您需要外部工具来查看虚拟机,可以使用 Java VisualVM(jvisualvm.exe) 等工具JConsole(jconsole.exe) 非常有用。

If you need it at Runtime from you code, something like console.log is provided by log4j. The information you'll log can be retrieved via Java Reflection

For dumping Java objects, look at the answers over here for various ways of doing it. I'd use XStream for its ease of use and maturity.

If you need external tools to look into the VM, tools like Java VisualVM(jvisualvm.exe) and JConsole(jconsole.exe) are very useful.

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