作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的 Java 代码中有一个简单的方法,例如
private int specialAdd (int a, int b) {
if(a < 100) {
return 100 * a + b;
} else {
return a + b;
}
}
有没有一种方法可以在 Eclipse 中使用给定的参数值“运行/调试选定的代码”?我真的很想只运行这个方法并查看 a
和 b
的几个 a 值的结果。
最佳答案
Is there a way to "run/debug selected code" with given parameter values in eclipse?
不,Eclipse 需要一个常规的 main
方法或一个 @Test
方法作为入口点。
当我想做一个“一次性”迷你测试时,我通常做的是
class YourClass {
private int specialAdd (int a, int b) {
if(a < 100) {
return 100 * a + b;
} else {
return a + b;
}
public static void main(String[] args) {
System.out.println(new YourClass().specialAdd(10, 100));
}
}
然后运行(或调试)当前类。 (专业提示:键入 main
并按 Ctrl+Space 将其展开为完整的 main 方法)
从 JDK 9 开始,您还可以使用 jshell
进行此类测试:
$ ./jshell
| Welcome to JShell -- Version 1.9.0-internal
| Type /help for help
-> int specialAdd(int a, int b) {
>> if (a < 100) {
>> return 100 * a + b;
>> } else {
>> return a + b;
>> }
>> }
| Added method specialAdd(int,int)
-> specialAdd(10, 5);
| Expression value is: 1005
| assigned to temporary variable $2 of type int
关于java - 如何在 Eclipse 中快速测试 Java 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27245914/
我是一名优秀的程序员,十分优秀!