簡單使用lombok 鏈式編程

使用條件

一,導包

    <!--只是用來下載一個jar-->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.12</version>
      <scope>provided</scope>
    </dependency>

二,安裝插件

File -> Settings -> Plugins 搜索Lombok,並進行安裝

 

以下參考自

首先來看一下不加Lombok如何實現鏈式

package 方;

class Student{
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public Student setAge(int age) {
        this.age = age;
        return this;
    }

    public String getName() {
        return name;
    }

    public Student setName(String name) {
        this.name = name;
        return this;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
public class Test {

    public static void main(String[] args) {
        Student student = new Student().setAge(18).setName("TAT");
        System.out.println( student.toString() );
    }
}

 

使用Lombok

package 方;


import lombok.*;
import lombok.experimental.Accessors;

@Accessors(chain = true)
@Data
class Student{
    private int age;
    private String name;
}
public class Test {

    public static void main(String[] args) {
        Student student = new Student().setAge(18).setName("TAT");
        System.out.println( student.toString() );
    }
}

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