Java有检查对象类型的方法吗?
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
检查 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.
您想要使用 Java 反射 API。
例如,试试这个:
You want to use the Java Reflection API.
For example, try this:
如果您在代码运行时需要它,则 提供类似
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 bylog4j
. The information you'll log can be retrieved via Java ReflectionFor 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.