英文:
Can I customize lombok to writing code for each variable in class similar to getters and setters?
问题
public class Test {
String t1;
String t2;
// These are the repetitive methods that I write for every variable that I declare.
public String TestT1() {
test(t1);
return t1;
}
public String TestT2() {
test(t2);
return t2;
}
private void test(String t1) {
System.out.println("For Example");
}
}
有相同的一组代码,我应该为我在类中声明的每个变量编写相似于getter和setter的代码。我可以自定义Lombok来做这个吗?还是有其他的方法可以做到这一点?
英文:
I have same set of code that I should write for each variable that I declare in class similar to getters and setters. Can I customize lombok to do this, or is there any other way to do this?
public class Test {
String t1;
String t2;
These are the repetitive methods that I write for every variable that I declare.
public String TestT1() {
test(t1);
return t1;
}
public String TestT2() {
test(t2);
return t2;
}
private void test(String t1) {
System.out.println("For Example");
}
}
答案1
得分: 0
你可以在Eclipse IDE中使用自定义模板来完成这个操作。虽然不是完全相同,但会有所帮助。
-
转到Windows > Preferences > Java > Editor > Templates。
-
点击New按钮,按照截图中的示例添加模板代码。
public String Test${arg:localVar}() {
test(${arg:localVar});
return ${arg:localVar};
} -
在Java编辑器中,按下Ctrl + Space,然后输入您的方法名。(在我的情况下,将使用在创建新模板时给出的TestMethods)(请参考下面的截图)
-
按下Enter键,Eclipse将自动添加创建的代码模板。在按下Enter键后,键入您的变量名。(请参考接下来的两个截图)
英文:
You can do it using Custom Templates in Eclipse IDE. Not exact same but it will help.
-
Go to Windows > Preferences > Java > Editor > Templates.
-
Click on New button add template code as given in screenshot.
public String Test${arg:localVar}() {
test(${arg:localVar});
return ${arg:localVar};
}
- In Java Editor, press Ctrl + Space and type your method name. (In my case it will be TestMethods as given while creating New Template) (Refer below screenshot)
- Press Enter, eclipse will automatically add code template as created. Type your variable name just after pressing enter. (Refer next two screenshots)
- After typing variable name.
专注分享java语言的经验与见解,让所有开发者获益!
评论