简体   繁体   中英

What is the difference between void method and return this

Class Player {
Player setName(String name){
this.name = name;
return this;

// or

void setName(String name){
this.name = name;
}}

Hi. What is the difference if I use the method with "void" or "return this" statement? Why the "return this" statement exists, if it does the same?

Why the "return this" statement exists, if it does the same?

They don't remotely do the same thing.

A void method has no return value. That means you can't use the return value (for instance, you can't assign it to a variable).

A method with a return value has a return value. In the particular case you've mentioned, return this , it's returning a reference to the object that the method was called on, so you can (potentially) use that reference — by assigning it to a variable, by calling another method on it, etc. This is useful for fluent interfaces (ones that allow you to do a lot of chaining):

theObject.doThis().thenDoThat().thenDoSomethingElse();

If it were void instead, you'd have to write that like this:

theObject.doThis();
theObject.thenDoThat();
theObject.thenDoSomethingElse();

Probably the most famous example of this 1 is Builder pattern of object construction, because it means you don't need a variable:

Thingy t = new ThingyBuilder()
    .withFoo("foo")
    .withBar("bar")
    .withBaz("baz")
    .build();

1 Most famous outside web development circles, that is; inside web development circles, the most famous example would be jQuery's API: $("div").css("color", "green").text("Good");

第一个用于链接方法(构建器模式),而第二个是普通的 setter 方法

When you return this , you return a Player instance.

More commonly used in the Builder Pattern, this returns a Player with the name of Joe.

Player p = new Player().setName("Bob").setName("Joe");

Using the void method, you cannot do the second setName . In order to rename the player, you'd have to get the first instance of the player object, then call setName again on the following line.

Obviously, a simple example, but is more useful when you have lots of setters like so

new Player()
  .setName("Bob")
  .setAge(42)
  .setGender("male")

Though, I think the convention is to use withX rather than setX here.

Returning this allows chaining such as:

player.setName(...).setSomethingElse(...)

When returning void there is no object reference so chaining is not possible and you have code such as:

player.setName(...); player.setSomethingElse(...);

I am wondering why one can ask how returning nothing and returning something could be the same. Anyway: no, returning something and returning nothing is not the same.

Returning this is mainly used to allow for "fluent" interfaces, so that users of your classes can write

yourObject.setFoo(foo).setBar(bar).doSomething();

instead of

yourObject.setFoo(foo);
yourObject.setBar(bar);
yourObject.doSomething();

If that helps with readability probably depends on the style you and the other people working with that source code are used to. One typical pattern that heavily relies on writing such code is the Builder pattern .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM