Need to clarify Java object initialization syntax

I am a programmer C\C++starting with Java.

I stumbled upon this working syntax of Java code, which I understand what it does, but I cannot understand the syntax logic.

object x = new object
            .SetContent(aaa)
            .SetIcon(bbb)
            .SetText(ccc);

I get the equivalent code C++:

object* x = new object;

x->SetContent(aaa);
x->SetIcon(bbb);
x->SetText(ccc);

Can someone explain me the logic of the Java syntax?
Is this something like a Visual Basic statement With?

PS Do not think that this is important, but the Java snippet is a program for Android.

+4
source share
5 answers

These chain calls are possible because each setter method returns a reference to this:

public object SetContent(final String input){
    this.aaa = input;
    return this;
}
+4
source

, , . Java Builder,

Foo foo = FooBuilder.builder()
    .setBar("bar")
    .setVolume(11)
    .setAnswer(42)
    .build();

.set___() -, build() Foo. :

FooBuilder builder = FooBuilder.builder();
builder = builder.setBar("bar");
builder = builder.setVolume(11);
builder = builder.setAnswer(42);
Foo foo = builder.build();

. ++, , .

EDIT: : http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ImmutableSet.Builder.html

0

'x', , Java. :

private object x = new object();
x.setContent(aaa);
x.setIcon(bbb);
x.setText(ccc);

, .

0

Object , . , this.

:

Object methodFirst(Object ob1)
{
ob1.doSomeOperations();
return this;
}

Object methodSecond(Object ob1)
{
ob1.doSomeOtherOperations();
return this;
}

:

Object newObject = oldObject.methodFirst().methodSecond(); 

0

All Articles