例如我有以下对象:
public class Dog{
private String name;
private int age;
private int legs;
private Color color;
/*getters and setters*/
}
我想初始化它并设置所有属性,而不是通过构造函数而是使用 setter :
Dog dog = new Dog();
dog.setName("Rex");;
dog.setAge(4);
...
我可以生成以简单的方式设置上面所有字段的代码吗?
最佳答案
这很麻烦,但我所做的是:
Use Eclipse's Source -> Generate Getters and Setters... function (also
ALT + SHIFT + S
) and then just replace all '=' characters with '(' and ';' with ');'. finally I go through every line and pressctrl-space
to let Eclipse finish the method call with proper casing (configure Eclipse to overwrite instead of insert code assist suggestions).
如果很多,或者进行正则表达式替换。
关于java - Eclipse,通过 setter 初始化对象的简短方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25906618/