mszhe的技术分享 人心惟危,道心惟微。惟精惟一,允执厥中。

java bean的set方法另一种方式

2016-08-17

阅读:


直接上示例:

private String name;
private int age;
private List<String> friends;
public Foo setName(String name) {
    this.name = name;
    return this;
}
public Foo setAge(int age) {
    this.age = age;
    return this;
}
public Foo setFriends(List<String> friends) {
    this.friends = friends;
    return this;
}

好处:

Foo foo = new Foo();
foo.setName("Tom")
   .setAge(25)
   .setFriends(Lists.newArrayList("Jack", "Lily"));

上一篇 Markdown语法