VScode Java代碼自動生成器

Java Code Generators

  • 這是一款Java自動生成代碼的插件,主要用於生成類裏的必需方法

    • Generate Setters & Getters
    • Generate toString()
    • Generate Constructor
    • Generate Constructor Using Fields
    • Generate Equals And HashCode
    • Generate Fluent Setters

安裝後右鍵代碼點擊GUI

示例

  • 寫好field的類
public class class1 {
    final int a, b;
    private int c, d;

}
  • 打開GUI
  • 界面
  • 可以把最下面的自動關閉取消
  • 選擇自己需要生成的代碼
  • 提示:該插件會重複生成

生成範例

  • 點擊Generate All
public class class1 {
    final int a, b;
    private int c, d;

    public class1() {
    }

    public class1(int a, int b, int c, int d) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    public int getA() {
        return this.a;
    }


    public int getB() {
        return this.b;
    }


    public int getC() {
        return this.c;
    }

    public void setC(int c) {
        this.c = c;
    }

    public int getD() {
        return this.d;
    }

    public void setD(int d) {
        this.d = d;
    }

    public class1 c(int c) {
        this.c = c;
        return this;
    }

    public class1 d(int d) {
        this.d = d;
        return this;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this)
            return true;
        if (!(o instanceof class1)) {
            return false;
        }
        class1 class1 = (class1) o;
        return a == class1.a && b == class1.b && c == class1.c && d == class1.d;
    }

    @Override
    public int hashCode() {
        return Objects.hash(a, b, c, d);
    }

    @Override
    public String toString() {
        return "{" +
            " a='" + getA() + "'" +
            ", b='" + getB() + "'" +
            ", c='" + getC() + "'" +
            ", d='" + getD() + "'" +
            "}";
    }

}

到此就生成完了,之後可以自己補充額外的部分

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章