eclipse 生成局部变量来接收一个方法eclipse的返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17808112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:56:50  来源:igfitidea点击:

Generate local variable to receive the return value of a method eclipse

javaeclipse

提问by Mohammed Subhi Sheikh Quroush

for example I have this code

例如我有这个代码

categoryCT.getInsertedItems();

and I want shortcut to generate code like this

我想要生成这样的代码的快捷方式

List<Category> insertedItems=   categoryCT.getInsertedItems();

回答by Aaron Digulla

Eclipse can't help you with the variable name but you can write:

Eclipse 无法帮助您使用变量名称,但您可以编写:

insertedItems = categoryCT.getInsertedItems();

This will give you a compile error.

这会给你一个编译错误。

If you press Ctrl+1anywhere in this line, Eclipse will offer "Create local variable 'insertedItems'"

如果您按此行中的Ctrl+1任意位置,Eclipse 将提供“创建局部变量 'insertedItems'”

Fewest keystrokes to get the desired result:

获得所需结果的最少击键次数:

  • catCTCtrl+Space-> categoryCT
  • .getIICtrl+Space-> categoryCT.getInsertedItems()
  • ;
  • Shift+Alt+Leftto select the whole method invocation
  • Ctrl+1+ select "Create new local variable"
  • catCTCtrl+Space-> categoryCT
  • .getIICtrl+Space-> categoryCT.getInsertedItems()
  • ;
  • Shift+Alt+Left选择整个方法调用
  • Ctrl+1+ 选择“创建新的局部变量”

回答by displayName

The shortcut that works on my Mac is ?2+ l(lowercase L key)

适用于我的 Mac 的快捷方式是?2+ l(小写 L 键)

Steps:

脚步:

  • Write the variable, Ex: categoryCT.getInsertedItems();;
  • Select the entire variable definition;
  • Press ?2, leave the press and then press l(lowercase L key);
  • 写入变量,例如:categoryCT.getInsertedItems();;
  • 选择整个变量定义;
  • ?2,离开按然后按l(小写 L 键);

This will create the line List<Category> insertedItems= categoryCT.getInsertedItems();.

这将创建线List<Category> insertedItems= categoryCT.getInsertedItems();

回答by Shanu Gupta

I'd add for windowsusers:

我会为Windows用户添加:

Press ctrl+2for the available options.

ctrl+2可获得可用选项。

For ex. ctrl+2+Lwill assign your statement to a local variable.

例如。ctrl+2+L将您的语句分配给局部变量。

回答by jeffery.yuan

Just uses eclipse's quick fix: Cmd+1. after type categoryCT.getInsertedItems(), then click Cmd+1 when the cursor is at the end.

只需使用 eclipse 的快速修复:Cmd+1。输入categoryCT.getInsertedItems()后,当光标在最后时点击Cmd+1。

回答by Ashish Agrawal Yodlee

Ctrl+2, L will do what you are looking for.

Ctrl+2, L 会做你想要的。

回答by Rakesh Singh Balhara

Alt + Shift + V:in NetBeansExtracts an existing expression or statement into a new variable. For example, the statement appears multiple times so it should be introduced to a variable:

Alt + Shift + V:NetBeans中将 现有表达式或语句提取到新变量中。例如,该语句出现多次,因此应将其引入变量:

textEmail.getText()

String text = textEmail.getText()

textEmail.getText()

字符串文本 = textEmail.getText()

Select statement then the Introduce Variable dialog appears. Introduce Variable dialog Enter the variable name and click OK.

Select 语句,然后出现 Introduce Variable 对话框。引入变量对话框 输入变量名称并单击确定。

Note that the scope of the newly introduced variable is local.

请注意,新引入的变量的作用域是局部的。