I'm using C++ and Java for a while and recently I'm on jquery. I noticed jquery's chain style functions. Then I think, if we could change setters in C++/Java just like jquery? For example, if our code is
class Person {
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Person setName(String name) {
this.name = name;
return this;
}
public Person setAge(int age) {
this.age = age;
return this;
}
}
Then we could write code as following instead:
Person person = new Person();
person.setName("Tom").setAge(20);
If we have lots of setters, this seems to be more simple.
I wonder if this is a good idea? Do you agree with me? Just give me your opinion. Thank you!