简体   繁体   English

JAVA方法调用方法

[英]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(); 像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(); getPositionOfClient().getTop(); or getPositionOfClient().getBottom(); which is something the JNA RECT class provides (top,bottom, left, right). 这是JNA RECT类提供的(顶部,底部,左侧,右侧)。 How do i do this? 我该怎么做呢?

Thanks in advance :) 提前致谢 :)

In order to make that possible, you need fluent APIs. 为了使之成为可能,您需要流利的 API。

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. 然后,当您的所有a()和b()都返回内容时,您可以写下这样的代码。

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 . 在这种情况下,这种做法意味着:违反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. 长话短说:不要仅仅为了乐趣而构建流畅的 API。 Consider carefully where they make sense; 仔细考虑它们有意义的地方; and never forget about the Law of Demeter! 永远不要忘记得墨meter耳定律!

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. 顶部,左侧,...是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! 您只需要使用“ .top”,例如,当您的方法已经返回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() . 仅当method()返回本身具有方法名称method()的类型时,这才是合法代码。 For your code, you just need whatever type getPositionOfTop() returns to itself have getTop() and getBottom() . 对于您的代码,您只需要getPositionOfTop()返回到其自身的任何类型,即具有getTop()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. 它不像您在另一个方法上调用method()一样。 For Ex: 例如:

You have a json string which is Mapped to a Pojo class named Demo. 您有一个映射到名为Demo的Pojo类的json字符串。 So now you are usong a Gson Class library of gson api which is used to convert json string to Class Object. 因此,现在您使用的是gson api的Gson类库,该库用于将json字符串转换为Class Object。

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

So Here Gson() which is a constructor method return Object of Gson class. 因此,这里的Gson()是Gson类的构造方法返回Object。

new Gson()

Now we can call the fromJson(String, ClassofT) method using the object returned by Gson Constructor. 现在,我们可以使用Gson构造函数返回的对象调用fromJson(String,ClassofT)方法。 This method convert the given json string to a Demo Class Object. 此方法将给定的json字符串转换为演示类对象。

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

Then we are calling the toString method of Demo class to print our information. 然后,我们调用Demo类的toString方法来打印我们的信息。 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. 如您所见,您可以在返回类型对象上调用一个方法,并且该方法对于该对象是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM