链式方法如何在Java中执行? [英] How does chained methods execute in java?

查看:86
本文介绍了链式方法如何在Java中执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

result = method1().method2().method3();

我想知道以上代码/语句的执行层次

I would like to know the execution hierarchy of the above code/statement

推荐答案

只需完成以下几点.

  1. 确定最左边的方法调用将返回什么(将其称为x).
  2. 使用x作为调用第二种方法(从左开始)的对象.如果有 只有两个链接方法,第二个方法调用的结果是 表达式的结果.
  3. 如果有第三个方法,则使用第二个方法调用的结果 调用第三个方法.
  1. Determine what the leftmost method call will return (let’s call it x).
  2. Use x as the object invoking the second (from the left) method. If there are only two chained methods, the result of the second method call is the expression's result.
  3. If there is a third method, the result of the second method call is used to invoke the third method.

根据您的陈述,执行层次如下:

As per your statement, the execution hierarchy will be as follows:

  1. 首先,将调用最左侧的方法 method1().
  2. 假设 method1()返回一个对象"meth",则第二种方法(从左侧开始) method2()将被称为meth. method2( ).
  3. 最后,从 method2()返回的对象将用于调用 method3().
  1. First , method1() which is the leftmost method will be called.
  2. Suppose method1() returns an object "meth" then the second method (from the left) method2() will be called as meth.method2().
  3. Last, the object returned from method2() will be used to call the method3().

希望它可以澄清您的疑问.

Hope it clarifies you doubt.

这篇关于链式方法如何在Java中执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆