method-chaining

How to do method chaining in Java? o.m1().m2().m3().m4()

倖福魔咒の 提交于 2019-12-17 06:33:34
问题 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? 回答1: This pattern is called "Fluent Interfaces" (see Wikipedia) Just return this; from the methods instead of returning nothing. So for example public void makeText(String text)

C++ Method chaining with classes

拈花ヽ惹草 提交于 2019-12-13 05:15:43
问题 I'm attempting to do method chaining, however, instead of using the Methods in "Foo" I want to a constructor of a class (which is inherited from the base class): class Bar { public: Bar() { std::cout << "This is bar"; } }; class Foo : public Bar { public: Foo() { cout << "This is foo"; } }; So my main would look like the following: Foo f = Foo().Bar(); Why is this not possible in C++/C++11? Also, is there a way in which I can integrate this standard, or would I have to create an method in

Fluent API and Method-Chaining Style Usage

时光怂恿深爱的人放手 提交于 2019-12-12 18:19:48
问题 When programming against a fluent API or just using method-chaining, I've seen the style mostly like this: var obj = objectFactory.CreateObject() .SetObjectParameter(paramName, value) .SetObjectParameter(paramName, value) .DoSomeTransformation(); What is the reasoning behind putting the dot at the beginning of the line instead of the end of the line like this: var obj = objectFactory.CreateObject(). SetObjectParameter(paramName, value). SetObjectParameter(paramName, value).

Is jQuery method chaining an example of fluent programming?

…衆ロ難τιáo~ 提交于 2019-12-12 11:53:03
问题 I'm somewhat new to JavaScript/jQuery, but when I saw examples of method chaining it struck me as instantly familiar. Other interfaces like LINQ do something similar where the return type of a set of methods is the same as the type they operate on (TweetSharp does something very similar). Is this an example of fluent programming? Much of what I read about jQuery says that other libraries have "borrowed" this idea of method chaining - did the idea originate with jQuery? 回答1: jQuery indeed

Dynamic chaining “thenReturn” in mockito

梦想的初衷 提交于 2019-12-12 10:33:11
问题 I have a Tuple mock class, whose getString(0) and getString(1) methods are expected to be called n times. Instead of writing something like, when(tuple.getString(0)).thenReturn(logEntries[0]).thenReturn(logEntries[1])...thenReturn(logEntries[n - 1]) manually, I tried the following: OngoingStubbing stubbingGetStringZero = when(tuple.getString(0)).thenReturn(serviceRequestKey); OngoingStubbing stubbingGetStringOne = when(tuple.getString(1)).thenReturn(logEntries[0]); for (int i = 1; i < n; i++)

PHP: Class property chaining in variable variables

北战南征 提交于 2019-12-12 09:56:31
问题 So, I have a object with structure similar to below, all of which are returned to me as stdClass objects $person->contact->phone; $person->contact->email; $person->contact->address->line_1; $person->contact->address->line_2; $person->dob->day; $person->dob->month; $person->dob->year; $album->name; $album->image->height; $album->image->width; $album->artist->name; $album->artist->id; etc... (note these examples are not linked together). Is it possible to use variable variables to call contact-

method chain a derived class method after calling a base class method

这一生的挚爱 提交于 2019-12-12 06:38:31
问题 With a class and a derivative as shown below, is there a way for the base classes methods to return a object reference of the derived type instead of its own type so syntactically i can chain the methods together? Suppose that object A has methods a1,a2 and derivative AD adds a method ad1 how would one go about making a valid method chain of AD_instance.a1().a2().ad1(); ? Below are the two classes in question. Ignoring what it's actually doing, the method chaining is the only important part.

Having two $.ajax() calls in one script

拜拜、爱过 提交于 2019-12-12 04:45:49
问题 I want to show some progress information of a server side procedure (which in fact is an ffmpeg re-encoding). The best way I though of was to use two different $.ajax() calls on myscript like this: 1) when the form is submitted (which would trigger the re-encoding) one $.ajax() would start the re-encoding like this: $.ajax({ type: "GET", url: "runffmpeg.php", data: dataString, success: function(data, textStatus, XMLHttpRequest) { //clearInterval(repeat); location.reload(); }}); 2) Then, a

method chaining with sub-methods

房东的猫 提交于 2019-12-12 03:17:22
问题 I am trying to use method chain with sub-methods. IE: foo("bar").do.stuff() The catch is stuff() needs to have a reference to the value of bar("bar") Is there any this.callee or other such reference to achieve this? 回答1: Is there any this.callee or other such reference to achieve this? No, you'd have to have foo return an object with a do property on it, which either: Make stuff a closure over the call to foo Have information you want from foo("bar") as a property of do , and then reference

PHP Last Object of Method Chaining

你。 提交于 2019-12-11 22:52:46
问题 In PHP using method chaining how would one go about supplying a functional call after the last method being called in the chain? Also while using the same instance (see below). This would kill the idea of implementing a destructor. The end result is a return value and functional call of private "insert()" from the defined chain properties (of course) without having to call it publicly, no matter of the order. Note, if I echo (__toString) the methods together it would retrieve the final