java 根据签名在 Netbeans 中自动创建构造函数体和字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11300827/
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-10-31 04:35:12  来源:igfitidea点击:

Auto create constructor body and fields in Netbeans based on signature

javanetbeans

提问by assylias

If I start from an empty class (TestClass) in Netbeans and add the following empty constructor:

如果我从 Netbeans 中的一个空类 (TestClass) 开始并添加以下空构造函数:

public void TestClass(String a, String b) {
}

is there a way to have netbeans automagically generate:

有没有办法让 netbeans 自动生成:

private final String a;
private final String b;

public void TestClass(String a, String b) {
    this.a = a;
    this.b = b;
}

I know that I can first create the 2 members and ask netbeans to auto-generate the constructor but I'm asking for the other way round.

我知道我可以先创建 2 个成员并要求 netbeans 自动生成构造函数,但我要求反过来。

For example, in eclipse, this can be achieved by pressing CTRL+1on the constructor's argument > assign parameter to new field.

例如,在 eclipse 中,这可以通过在构造函数的参数上按CTRL+ 1> 将参数分配给新字段来实现。

采纳答案by Puce

You can write the empty constructor with the required signature. Then set the cursor next to a parameter and press Alt+ENTER.

您可以使用所需的签名编写空构造函数。然后将光标设置在参数旁边并按 Alt+ENTER。

NetBeans will ask to create a new field. Press ENTER and NetBeans will write the code for you.

NetBeans 将要求创建一个新字段。按 ENTER 键,NetBeans 将为您编写代码。

I think you have to do it for each parameter separately, but I'm not sure.

我认为您必须分别为每个参数执行此操作,但我不确定。

Generally, Alt+ENTER in NetBeans is similar to Ctrl+1 in Eclipse, also at other places.

一般来说,NetBeans 中的 Alt+ENTER 类似于 Eclipse 中的 Ctrl+1,其他地方也一样。

回答by Ali

from: https://coderwall.com/p/oyanzg

来自:https: //coderwall.com/p/oyanzg

Just point the cursor at the point where you want the generated code to appear, and press Alt + Insert (or select Source -> Insert Code). The following menu will appear, where you can choose to generate whatever you want:

只需将光标指向您希望生成的代码出现的位置,然后按 Alt + Insert(或选择 Source -> Insert Code)。将出现以下菜单,您可以在其中选择生成任何您想要的内容:

enter image description here

在此处输入图片说明