Java中如何进行方法链? o.m1().m2().m3().m4()
我在许多 Java 代码符号中看到,在一个方法之后我们调用另一个方法,下面是一个例子。
Toast.makeText(text).setGravity(Gravity.TOP, 0, 0).setView(layout).show();
正如您所看到的,在返回时调用 makeText
后,我们调用 setGravity
,到目前为止,
我如何使用自己的类来做到这一点?我需要做一些特别的事情吗?
I've seen in many Java code notation that after a method we call another, here is an example.
Toast.makeText(text).setGravity(Gravity.TOP, 0, 0).setView(layout).show();
As you see after calling makeText
on the return we call setGravity
and so far
How can I do this with my own classes? Do I have to do anything special?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这种模式称为“Fluent Interfaces”(请参阅维基百科)
只需
返回此; 来自方法而不是返回任何内容。
例如
会变成
This pattern is called "Fluent Interfaces" (see Wikipedia)
Just
return this;
from the methods instead of returning nothing.So for example
would become
没有方法链接
方法链接也称为命名参数惯用语,是面向对象编程语言中调用多个方法的常见语法。每个方法返回一个对象,允许在单个语句中将调用链接在一起。链接是语法糖,消除了对中间变量的需要。方法链也称为火车残骸,因为随着更多方法链接在一起,同一行中的方法数量会不断增加,即使方法之间经常添加换行符。
类似的语法是方法级联,其中在方法调用之后,表达式的计算结果为当前对象,而不是方法的返回值。级联可以使用方法链来实现,方法是返回当前对象本身(this)。级联是流畅接口中的一项关键技术,由于链接在面向对象语言中广泛实现,而级联则不然,因此这种形式的“通过返回 this 进行级联”通常简称为“链接”。链接和级联都来自 Smalltalk 语言。
Without method chaining
Method chaining, also known as named parameter idiom, is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement. Chaining is syntactic sugar which eliminates the need for intermediate variables. A method chain is also known as a train wreck due to the increase in the number of methods that come one after another in the same line that occurs as more methods are chained together even though line breaks are often added between methods.
A similar syntax is method cascading, where after the method call the expression evaluates to the current object, not the return value of the method. Cascading can be implemented using method chaining by having the method return the current object itself (this). Cascading is a key technique in fluent interfaces, and since chaining is widely implemented in object-oriented languages while cascading isn't, this form of "cascading-by-chaining by returning this" is often referred to simply as "chaining". Both chaining and cascading come from the Smalltalk language.
从你的例子来看:
链中的每个方法都必须返回一个类或一个接口。链中的下一个方法必须是返回类的一部分。
我们从吐司开始。 makeText 方法在 Toast 类中定义为静态方法,必须返回一个类或接口。在这里,它返回 Gravity 类的一个实例。
Gravity类中定义的方法setGravity返回类View的实例。View
类中定义的方法setView返回类JPanel的实例。
这条链可以一步步写出来。
将链写为链会从源代码中删除所有中间实例变量。
From your example:
Each method in the chain has to return a class or an interface. The next method in the chain has to be a part of the returned class.
We start with Toast. The method makeText, which is defined as a static method in the class Toast, has to return a class or an interface. Here, it returns an instance of the class Gravity.
The method setGravity, which is defined in the class Gravity, returns an instance of the class View,
The method setView, which is defined in the class View, returns an instance of the class JPanel.
This chain could be written out step by step.
Writing the chain as a chain removes all of the intermediate instance variables from the source code.
在谷歌上搜索构建器模式或流畅的界面以了解更多详细信息。
在大多数情况下,在方法末尾返回“this”就可以解决问题。
Search for builder pattern or fluent interface on google to have more details about this.
Return 'this' at the end of your method can do the trick in most cases.
添加返回此;肯定有助于此类的链接,但对于子类则失败。
如果您希望将链接行为继承到子类,则也可以更改您的类签名,如下所示:
Class SuperClass < SubClass extends SuperClass >{}
这样所有的子类都会继承方法链。
例子:
Adding return this; would surely help in chaining for this class but fail for sub-classes.
If you want to have the chaining behaviour inherited to the sub-classes also then change your class signature as below:
Class SuperClass < SubClass extends SuperClass >{}
This way all sub-classes will inherit method chaining.
Example:
或者您可以使用 Diezel 来根据您的流利的正则表达式生成您需要的所有接口API。
or you can use Diezel that generates all the interfaces you need based on a Regular expression of your fluent API.