简体   繁体   English

在Intellij IDEA中使用热键自动生成返回对象的类型和引用

[英]Automatically generating of type and reference to returned object for method using hot keys in Intellij IDEA

How can I get the type automatically of returned object for method using hotkeys in Intellij IDEA ? 如何使用Intellij IDEA中的热键自动获取返回对象的类型?

Quite often there are times when you need to modify such line: 有时候你需要修改这样的行:

myinstance.getMyMethod();

in the following: 在下面的:

IMySomeObject mysomeobject = myinstance.getMySomeObject();

I wish that IDEA did it itself for me to save time. 我希望IDEA能够为我节省时间。 I do not wish to explore method's signature, find its returned type and manually create this reference with a specific type of returned object. 我不希望探索方法的签名,找到它返回的类型,并使用特定类型的返回对象手动创建此引用。 It's not convenient. 这不方便。

Using the Introduce Variable refactor. 使用Introduce Variable重构器。

Select 选择

myinstance.getMyMethod(); 

press <ctrl>+<alt> + V and you will see a selections of names to give it like <ctrl> + <alt> + V ,您将看到一些名称的选择

IMySomeObject mySomeObject = myinstance.getMySomeObject();

I then select <Enter> as the first option is usually fine. 然后我选择<Enter>,因为第一个选项通常很好。

I suggest you have a look at all the refactoring tools in the Refactor Menu and learn what they all do. 我建议您查看Refactor菜单中的所有重构工具,并了解它们的作用。


You can type m 你可以输入m

在此输入图像描述

then type . 然后键入

在此输入图像描述

then <Enter> 然后<Enter>

在此输入图像描述

then press <ctrl> + <alt> + V 然后按<ctrl> + <alt> + V.

在此输入图像描述

lastly press <Enter> to accept the default name. 最后按<Enter>键接受默认名称。

Another way besides Introduce Variable is by using their postfix completion feature. 除了Introduce Variable之外的另一种方法是使用它们的后缀完成功能。

The key thing is to type .var after the expression and then press the tab key. 关键是在表达式后输入.var然后按Tab键。

Eg 例如

myinstance.getMyMethod().var

With your cursor right after .var , press ctrl + space , and then press tab to select the suggestion. 将光标放在.var之后,按ctrl + space ,然后按Tab键选择建议。 You'll then end up with: 然后你会得到:

IMySomeObject mySomeObject = myinstance.getMySomeObject();

btw, there's many more postfix completions options; 顺便说一句,还有更多的postfix完成选项; I find it to be a very handy feature. 我发现这是一个非常方便的功能。

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

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