如何在emacs中为Java类实现“生成getter / setter”? [英] How to implement a 'generate getter/setter' for a Java Class in emacs?
问题描述
有时候,我想念使用IDE的懒惰,让我只是写一个Java类的属性,然后让IDE生成所需的getter / setter。
Sometimes I miss the laziness of using an IDE that let me just write the attribute of a Java class and then let the IDE generate the required getter/setter.
Emacs可以这样做吗?
Can Emacs do this?
目前我只是复制粘贴一个上一行的getter / setter,然后复制粘贴并进行修改。很简单,但是使编码有点有趣:)
Currently I just copy paste a pair of getter/setter form the previous line, and then copy paste and modify it. It's simple, but yet, make coding a little bit funnier :)
推荐答案
您具体询问了如何生成一个getter / setter对。你可以写elisp来做这个。但是看一个更通用的解决方案可能很有趣。
You asked specifically about generating a getter/setter pair. And you can write elisp to do this. But it may be interesting to look into a more general solution.
为了解决这个问题,我使用 ya -sippip 。该名称指的是另一个Snippet包,所以你可以确定问题已经解决了。但是,为了满足我的需要,我发现ya-snippet是最有用,最简单和最有能力的解决方案。
To solve this generally, I use ya-snippet . The name refers to "Yet Another Snippet package", so you can be sure the problem has been solved before. But I found ya-snippet to be the most useful, simple, and capable solution, for my needs.
对于具有getter / setter的属性,我键入
For a property with a getter/setter, I type
prop<TAB>
...我收到一个我可以填写的模板,就像一个表单。我指定属性的名称,并生成其他所有内容。很好,很容易
...and I get a template that I can then fill in, like a form. I specify the name of the property, and everything else is generated. Very nice, easy.
这适用于您通常在代码中使用的任何微图案。我有一个单例,构造函数,循环,switch语句,try / catch等的片段。
This works for any micro-pattern you commonly use in code. I have snippets for a singleton, constructor, for loops, switch statements, try/catch, and so on.
使用ya-snippet的键是没有编写elisp代码。基本上我只是提供了模板的文本,它的工作原理。这是您上面看到的getter / setter片段的ya-snippet代码:
The key with ya-snippet is there is no elisp code to write. Basically I just provide the text for the template, and it works. This is the ya-snippet code for the getter/setter snippet you see above:
# name : getter/setter property ... { ... }
# key: prop
# --
private ${1:Type} _${2:Name};
public ${1:Type} get$2 {
${3://get impl}
}
public void set$2($1 value) {
${4://set impl}
}
# - 是剪辑的元数据。 关键是元数据中最重要的一点 - 它是可以扩展的短序列。该名称显示在yasnippet菜单上。 # -
行之下的内容是扩展代码。它包括几个填写字段。
Everything above the "# --" is metadata for the snip. The "key" is the most important bit of that metadata - it is the short sequence that can be expanded. The name is shown on the yasnippet menu. The stuff below the # --
line is the expansion code. It includes several fill-in fields.
YAsnippet适用于emacs(java,php,c#,python等)中的任何编程模式,也适用于其他文本模式。
YAsnippet works for any programming mode in emacs (java, php, c#, python, etc) and it works for other text modes too.
这篇关于如何在emacs中为Java类实现“生成getter / setter”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!