是否有在Eclipse中向现有构造函数添加字段的快捷方式? [英] Is there a shortcut for adding fields to existing constructor in Eclipse?
问题描述
Eclipse中是否有任何快捷方式允许我将字段添加到现有构造函数的参数列表中?
Is there any shortcut in Eclipse that allows me to add a field to the argument list of an existing constructor?
示例:
我有这个类:
public class A {
int a;
int b;
public A(int a, int b) {
this.a = a;
this.b = b;
}
}
当我添加字段 int c
(或许多字段)我想添加它到构造函数的参数列表并将参数分配给字段:
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;
}
}
我目前通过手动创建参数然后执行此操作按 CTRL + 1
然后选择将参数分配给字段
i currently do this by creating the parameter manually and then press CTRL + 1
and then choose "assign parameter to field"
但是如果我添加了多个字段一旦这不是一个真正好的解决方案imho。
but if i add more than one field at once this isn't really a good solution imho.
我不想创建新的构造函数!
推荐答案
我会首先使用更改方法签名重构(mac上的选项+命令+ c)将额外参数添加到构造函数中。这样,调用构造函数的现有代码可以将合理的默认值作为参数传递(如果您愿意)。然后按CTRL + 1选择多次,以便按照建议将新字段快速修复到课程中。
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.
这篇关于是否有在Eclipse中向现有构造函数添加字段的快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!