简体   繁体   English

如何在Eclipse的调试控制台中打印Java对象的当前值?

[英]How to print Java object's current value in debug console in Eclipse?

I'm newbie on Eclipse. 我是Eclipse的新手。

In Objective-C, I could print value of an object in console window with this command. 在Objective-C中,我可以使用此命令在控制台窗口中打印对象的值。

po nameOfValue

Maybe gdb command. 也许是gdb命令。 I'm using Eclipse now, what's the equivalent of this in Eclipse? 我现在正在使用Eclipse,这与Eclipse中的相​​同之处是什么?

PS I'm debugging a Java app. PS我正在调试一个Java应用程序。

Eclipse has very robust debugging capabilities - much more so than Objective C. Eclipse具有非常强大的调试功能 - 远远超过Objective C.

First off, while debugging you can view the values of all variables in the Variables window. 首先,在调试时,您可以在“变量”窗口中查看所有变量的值。 Additionally, in the lower part of the Variables window you can type arbitrary Java, select it, right click, then choose to Inspect or Execute. 此外,在“变量”窗口的下半部分,您可以键入任意Java,选择它,右键单击,然后选择“检查”或“执行”。 You can actually change the value of variables in your program this way, while its running. 实际上,您可以在运行时以这种方式更改程序中变量的值。

You can do pretty much the same thing in your source pane. 您可以在源窗格中执行相同的操作。 Highlight a variable, right click and choose to Inspect it. 突出显示变量,右键单击并选择“检查”。 You can also type in a random expression and execute it. 您还可以键入随机表达式并执行它。 You can also places watches on variables (which I believe you can do in Objective-C), or on expressions. 您还可以将监视放在变量上(我相信您可以在Objective-C中执行)或表达式。

There is an Expression view which is not displayed by default (on your menu select Window->Views->Expressions, while in Debug perspective). 有一个表达式视图默认不显示(在菜单中选择Window-> Views-> Expressions,而在Debug透视图中)。 It allows you to add arbitrary (valid) Java expressions and the values of those expressions will then be watched over the lifetime of your debug session, very nifty. 它允许您添加任意(有效)Java表达式,然后在调试会话的生命周期中监视这些表达式的值,非常漂亮。 Thanks to @Baldrick for the reminder of this great tool. 感谢@Baldrick提醒这个伟大的工具。

System.out.println(nameOfValue);

I'm not aware of any printing option of the whole object state in console while debugging. 在调试时,我不知道控制台中整个对象状态的任何打印选项。

But you can override the toString() method of your object and there concatenate the string with the values of each field or whatever you want to print for that object. 但是您可以覆盖对象的toString()方法,并将字符串与每个字段的值或您要为该对象打印的任何内容连接起来。 Then when calling somewhere in the code System.out.print(myObject); 然后在代码System.out.print(myObject);调用某处时System.out.print(myObject); it will print the result of the toString() method which you've overridden. 它将打印您已覆盖的toString()方法的结果。

Click Window menu select show view type display and show that view. 单击窗口菜单,选择显示视图类型显示并显示该视图。 In this view you can type java such as System.out.println(objectName); 在此视图中,您可以键入java,例如System.out.println(objectName); when the application has paused during debugging. 当应用程序在调试期间暂停时。

Override toString for that object/class. 覆盖该对象/类的toString。 Then System.out.println(objectVariable). 然后是System.out.println(objectVariable)。

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

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