我可以通过构造函数在eclipse中自动生成字段吗? [英] Can I generate fields automatically in eclipse from a constructor?
问题描述
当我在Eclipse中进行编码时,我希望尽可能地懒惰。因此,我经常键入类似的内容:
When I'm coding in eclipse, I like to be as lazy as possible. So I frequently type something like:
myObject = new MyClass(myParam1,myParam2,myParam3);
myObject = new MyClass(myParam1, myParam2, myParam3);
Even尽管MyClass不存在,它也不是构造函数。几次单击之后,eclipse用从我键入的内容推断出的构造函数创建了MyClass。我的问题是,是否也可以使eclipse生成类中与我传递给构造函数相对应的字段?我意识到这是超级懒惰,但这就是日食的全部乐趣!
Even though MyClass doesn't exist and neither does it's constructor. A few clicks later and eclipse has created MyClass with a constructor inferred from what I typed. My question is, is it possible to also get eclipse to generate fields in the class which correspond to what I passed to the constructor? I realize it's super lazy, but that's the whole joy of eclipse!
推荐答案
如果您有A类。
class A{
A(int a |){}
}
|是光标。 Crtl + 1 将参数分配给新字段
结果:
class A{
private final int a;
A(int a){
this.a = a;
}
}
此方法也适用于方法:
void method(int b){}
将导致:
private int b;
void method(int b){
this.b = b;
}
这篇关于我可以通过构造函数在eclipse中自动生成字段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!