更新时间:2023-01-31 12:09:43
我将使用更改方法签名重构(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.