How to implement a 'generate getter/setter' for a Java Class in emacs?

后端 未结 4 782
别跟我提以往
别跟我提以往 2021-02-07 10:27

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.

Can Emacs do

4条回答
  •  终归单人心
    2021-02-07 10:53

    If you use the java-mode YASnippets by nekop you get the snippet prop which lets you define a private variable and it automatically makes a getter and a setter for that variable. The snippet reads as follows:

    # -*- mode: snippet -*-
    # name: property
    # key: prop
    # --
    private ${1:String} ${2:name};$0
    
    public $1 get${2:$(upcase-initials text)}() {
        return $2;
    }
    
    public void set${2:$(upcase-initials text)}($1 $2) {
        this.$2 = $2;
    }
    

    As can be seen this snippet does not differ much from the other answers except that it may be better formatted. Another advantage is that it is part of a package of snippets for Java.

提交回复
热议问题