如何在Java中进行方法链接?o.m1().平方米().立方米().M4()

Pen*_*m10 36 java method-chaining

我在许多Java代码表示法中看到,在我们调用另一个方法之后,这是一个例子.

Toast.makeText(text).setGravity(Gravity.TOP, 0, 0).setView(layout).show();
Run Code Online (Sandbox Code Playgroud)

正如你所看到的那样,makeText我们打电话回来setGravity,到目前为止

我怎么能用我自己的课程做到这一点?我必须做一些特别的事吗?

Tho*_*zer 87

这种模式称为"流畅的接口"(见维基百科)

只是return this;从方法而不是返回任何东西.

所以举个例子

public void makeText(String text) {
    this.text = text;
}
Run Code Online (Sandbox Code Playgroud)

会成为

public Toast makeText(String text) {
    this.text = text;
    return this;
}
Run Code Online (Sandbox Code Playgroud)

  • +1我多次使用这个但我不知道这是一个模式并且有一个名字. (4认同)

sub*_*his 10

class PersonMethodChaining {
private String name;
private int age;

// In addition to having the side-effect of setting the attributes in question,
// the setters return "this" (the current Person object) to allow for further chained method calls.

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

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

public void introduce() {
    System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
}

// Usage:
public static void main(String[] args) {
    PersonMethodChaining person = new PersonMethodChaining();
    // Output: Hello, my name is Peter and I am 21 years old.
    person.setName("Peter").setAge(21).introduce();
}
Run Code Online (Sandbox Code Playgroud)

}

没有方法链接

   class Person {
    private String name;
    private int age;

    // Per normal Java style, the setters return void.

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

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

    public void introduce() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
    }

    // Usage:
    public static void main(String[] args) {
        Person person = new Person();
        // Not using chaining; longer than the chained version above.
        // Output: Hello, my name is Peter and I am 21 years old.
        person.setName("Peter");
        person.setAge(21);
        person.introduce();
    }
}
Run Code Online (Sandbox Code Playgroud)

方法链(也称为命名参数idiom)是用于在面向对象的编程语言中调用多个方法调用的常用语法.每个方法都返回一个对象,允许在单个语句中将调用链接在一起.链接是语法糖,消除了对中间变量的需要.方法链也被称为火车残骸,因为随着更多方法被链接在一起而在相同的线路中一个接一个地出现的方法的数量增加,即使在方法之间经常添加断线.

类似的语法是方法级联,其中在方法调用之后,表达式求值为当前对象,而不是方法的返回值.可以使用方法链接来实现级联,方法是返回当前对象本身(this).级联是流畅接口中的关键技术,并且由于链接在面向对象语言中被广泛实现而级联不是,因此这种"通过返回链接逐级链接"的形式通常被简称为"链接".链接和级联都来自Smalltalk语言.


Gil*_*anc 6

从你的例子:

Toast.makeText(text).setGravity(Gravity.TOP, 0, 0).setView(layout).show();

链中的每个方法都必须返回一个类或一个接口。链中的下一个方法必须是返回类的一部分。

我们从吐司开始。方法 makeText 在类 Toast 中定义为静态方法,必须返回一个类或一个接口。在这里,它返回一个 Gravity 类的实例。

在类 Gravity 中定义的方法 setGravity 返回类 View 的实例,

在类 View 中定义的方法 setView 返回 JPanel 类的一个实例。

这个链可以一步一步写出来。

Gravity gravity = Toast.makeText(text);
View view       = gravity.setGravity(Gravity.TOP, 0, 0);
JPanel panel    = view.setView(layout);
panel.show();
Run Code Online (Sandbox Code Playgroud)

将链编写为链会从源代码中删除所有中间实例变量。


Gui*_*ume 5

在 google 上搜索 builder pattern 或 fluent interface 以获得更多详细信息。

在大多数情况下,在您的方法末尾返回 'this' 可以解决问题。