使代碼更簡潔(三) ----利用方法鏈爲對象屬性賦值

博客搬移到這裏:http://yemengying.com/
有個本身的博客還蠻好玩的,bazinga!app

有一個有不少屬性的類:函數

public class User {
    private int id;
    private String name;
    private int age;
    private int sex;
    private int cityId;
    private int buId;
    private int roleId;
    private String pinyinName;
    public String getPinyinName() {
        return pinyinName;
    }
    public void setPinyinName(String pinyinName) {
        this.pinyinName = pinyinName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getSex() {
        return sex;
    }
    public void setSex(int sex) {
        this.sex = sex;
    }
    public int getCityId() {
        return cityId;
    }
    public void setCityId(int cityId) {
        this.cityId = cityId;
    }
    public int getBuId() {
        return buId;
    }
    public void setBuId(int buId) {
        this.buId = buId;
    }
    public int getRoleId() {
        return roleId;
    }
    public void setRoleId(int roleId) {
        this.roleId = roleId;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public User(int id, String name, int age, int sex, int cityId, int buId,
            int roleId, String pinyinName) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.cityId = cityId;
        this.buId = buId;
        this.roleId = roleId;
        this.pinyinName = pinyinName;
    }
    
    
}

在爲它的屬性賦值時,一般有兩種方式,使用構造函數和使用set方法。但是使用構造函數有時會忘了各個字段的順序 ,直接使用set方法,又比較麻煩。因此同事提出能夠使用方法鏈,相似於StringBuilder的append方法ui

String s = new StringBuilder().append("0").append(1)
  .append(" 2 ").append(3).toString();

讓bean的每一個屬性的set方法都返回一個對象自己的引用,將User類的set方法改寫成下面的樣子:this

public User setId(int id) {
        this.id = id;
        return this;
    }

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

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

    public User setSex(int sex) {
        this.sex = sex;
        return this;
    }

    public User setCityId(int cityId) {
        this.cityId = cityId;
        return this;
    }

    public User setBuId(int buId) {
        this.buId = buId;
        return this;
    }

    public User setRoleId(int roleId) {
        this.roleId = roleId;
        return this;
    }

    public User setPinyinName(String pinyinName) {
        this.pinyinName = pinyinName;
        return this;
    }

這樣在對User的屬性賦值時就簡潔了許多。code

User user = new User().setId(1).setAge(18)
                        .setBuId(127)
                        .setRoleId(12)
                        .setName("giraffe")
                        .setCityId(12)
                        .setSex(1)
                        .setPinyinName("gif");

不過不知道這樣寫會不會有什麼很差的地方~~對象

相關文章
相關標籤/搜索