简体   繁体   中英

JAVA method calling upon method

I think i confused myself a little bit - anyways, how do i call a method upon another method. Like method().method().method(); ? I'd like to create a similar method myself for this sample code:

public RECT getPositionOfClient() {
    HWND client = User32.INSTANCE.FindWindow("classname", "client");
    RECT rect = new RECT();
    User32.INSTANCE.GetWindowRect(client , rect);
    System.out.println("rect = " + rect);
    return rect;
}

Here i'd like to be able call getPositionOfClient().getTop(); or getPositionOfClient().getBottom(); getPositionOfClient().getTop(); or getPositionOfClient().getBottom(); which is something the JNA RECT class provides (top,bottom, left, right). How do i do this?

Thanks in advance :)

In order to make that possible, you need fluent APIs.

Meaning: each of your methods ... has to return some object ; like:

Whatever a() { ... does something ... and 
 return whatEverInstance;
}

and then, when all your a()s, and b()s return something, you can write down such code.

And if you "stay" within the same class , and each method is just doing a

return this;

so you keep calling methods on the same object; than that is a somehow acceptable practice.

For any other cases: you should be really careful about doing that! As this practice in that case means: violating Law of Demeter .

The point is: a class should only know about its direct neighbors. You don't want to write code like

myField.doSomething().inSomeOtherClass().andNow2HopsAway() ...

because that couples this code to a completely different class.

Long story short: don't build fluent APIs just for the fun of it. Consider carefully where they make sense; and never forget about the Law of Demeter!

EDIT; after reading your questions and comments more thoroughly; I think you not only confused yourself, but also us:

as of now

public RECT getPositionOfClient() { ... returns an instance of RECT }

And top, left, ... are fields on RECT.

So you get

int whatever = foo.getPositionOfClient().top;

Done. It is a field; you don't need anything else but use ".top" for example when your method is already returning a RECT!

You don't call a method upon another method, you call a method on the return value of another method. method().method().method(); is only legal code if method() returns a type that itself has a method names method() . For your code, you just need whatever type getPositionOfTop() returns to itself have getTop() and getBottom() .

Methods are called of an object. The thing you call a method/property on, must be an object. If you want to do method chaining, each method should return a value that is actually an object. (Except the last method). Classes that are designed to be usable for method chaining just return themselves as the return value of a method.

Its not like you call method() upon another method. For Ex:

You have a json string which is Mapped to a Pojo class named Demo. So now you are usong a Gson Class library of gson api which is used to convert json string to Class Object.

new Gson().fromJson(inputString,Demo.class).toString().

So Here Gson() which is a constructor method return Object of Gson class.

new Gson()

Now we can call the fromJson(String, ClassofT) method using the object returned by Gson Constructor. This method convert the given json string to a Demo Class Object.

new Gson().fromJson(inputString,Demo.class)

Then we are calling the toString method of Demo class to print our information. So as you can see that you can call a method on a return type object and is valid if that method exists for that object.

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