向现有构造函数添加字段的快捷方式 [英] Shortcut for adding fields to existing constructor
问题描述
示例:是否有任何快捷方式允许我将添加一个字段到现有构造函数的参数列表?
我讨厌这个课程:
public class A {
int a;
int b;
public A(int a,int b){
this.a = a;
this.b = b;
}
}
当我添加一个字段 int c
(或许多字段)我想要将添加到构造函数的参数列表中,并将参数分配给字段:
public class A {
int a;
int b;
int c; //这是新的
public A(int a,int b,int c){
this.a = a;
this.b = b;
this.c = c;
}
}
我现在手动创建参数,然后按 CTRL + 1
,然后选择将参数分配给字段
但如果我添加多个字段一旦这不是一个很好的解决方案imho。
我不想创建一个新的构造函数!
我将使用更改方法签名重构(Mac上的选项+命令+ c)将额外的参数添加到构造函数。这样调用构造函数的现有代码可以将合理的默认值作为参数传递(如果您愿意)。然后选择多次CTRL + 1可以按照您的建议将新字段快速修复到课堂。
Is there any shortcut that allows me to add a field to the argument list of an existing constructor?
Example:
I hava this class:
public class A {
int a;
int b;
public A(int a, int b) {
this.a = a;
this.b = b;
}
}
when i add a field int c
(or many fields) i want to add it to the argumentlist of the constructor and assign the parameter to the field:
public class A {
int a;
int b;
int c; //this is new
public A(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
}
i currently do this by creating the parameter manually and then press CTRL + 1
and then choose "assign parameter to field"
but if i add more than one field at once this isn't really a good solution imho.
I don't want to create a new constructor!
I would use the "change method signature" refactoring first (option+command+c on mac) to add the extra parameter(s) to the constructor. This way existing code that calls the constructor can pass sensible default as parameters (if you like). Then choose as many times CTRL+1 to quick fix the new fields into the class as you suggested.
这篇关于向现有构造函数添加字段的快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!