学习 JAVA 需要帮助理解 getLogger().info()(方法链接)的概念
Learning JAVA need help understanding the concept of getLogger().info() (Method Chaining)
我遇到了以下代码行:
getLogger().info("Text Goes Here"); // This command outputs the text to console
我了解对象如何工作和被调用的基础知识,但在我在 youtube 上观看的视频中,vivz(作者)从未解释过在方法内部调用方法(至少我认为这是发生在上面的代码)。
他解释了
ClassName.variable
xVariable.method()
但与
无关
someMethod().anotherMethod();
用初学者的话来说,谁能解释一下这个概念或对这里发生的事情的一些解释?
基本上,我想知道 info()
是否是 getLogger()
中的方法,或者在这种情况下 getLogger()
和 info()
是什么?
getLogger()
方法 returns 和 Object
。然后调用 info()
方法。这是一种称为方法链接的 shorthand 方式。如果你愿意,你可以把它分成两行。
Logger logger = getLogger();
logger.info("Text goes Here");
这与 `getLogget().info("Text goes here");
Method chaining 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 without requiring variables to store the intermediate results.
这不是方法中的方法,这是两个方法调用。首先调用 getLogger()
,然后在 return 值(对象)上调用下一个 info(String)
。您在一条语句中链接了两个方法调用。
我会给你一个例子来理解这一点 String.concat()
:
public static void main(String[] args) {
String first = "First";
// Storing the resulting instance and then call again.
String s1 = first.concat(" Second");
s1 = s1.concat(" Third.");
System.out.println(s1);
// This is method chaining using String class with concat() method.
String s2 = first.concat(" Second").concat(" Third.");
System.out.println(s2);
}
此处String.concat()
return是String
拼接后的实例。现在您可以选择将实例存储在一个变量中,然后再次调用 concat()
或直接使用相同的实例。
调用 getLogger().info("Text Goes Here");
时会发生什么?首先,使用 getLogger()
调用一个方法,该方法 returns 是一个记录器对象 (class)。在第二步中,您对记录器对象调用 info("Text goes Here")
。链式方法调用很常见。
我遇到了以下代码行:
getLogger().info("Text Goes Here"); // This command outputs the text to console
我了解对象如何工作和被调用的基础知识,但在我在 youtube 上观看的视频中,vivz(作者)从未解释过在方法内部调用方法(至少我认为这是发生在上面的代码)。
他解释了
ClassName.variable
xVariable.method()
但与
无关someMethod().anotherMethod();
用初学者的话来说,谁能解释一下这个概念或对这里发生的事情的一些解释?
基本上,我想知道 info()
是否是 getLogger()
中的方法,或者在这种情况下 getLogger()
和 info()
是什么?
getLogger()
方法 returns 和 Object
。然后调用 info()
方法。这是一种称为方法链接的 shorthand 方式。如果你愿意,你可以把它分成两行。
Logger logger = getLogger();
logger.info("Text goes Here");
这与 `getLogget().info("Text goes here");
Method chaining 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 without requiring variables to store the intermediate results.
这不是方法中的方法,这是两个方法调用。首先调用 getLogger()
,然后在 return 值(对象)上调用下一个 info(String)
。您在一条语句中链接了两个方法调用。
我会给你一个例子来理解这一点 String.concat()
:
public static void main(String[] args) {
String first = "First";
// Storing the resulting instance and then call again.
String s1 = first.concat(" Second");
s1 = s1.concat(" Third.");
System.out.println(s1);
// This is method chaining using String class with concat() method.
String s2 = first.concat(" Second").concat(" Third.");
System.out.println(s2);
}
此处String.concat()
return是String
拼接后的实例。现在您可以选择将实例存储在一个变量中,然后再次调用 concat()
或直接使用相同的实例。
调用 getLogger().info("Text Goes Here");
时会发生什么?首先,使用 getLogger()
调用一个方法,该方法 returns 是一个记录器对象 (class)。在第二步中,您对记录器对象调用 info("Text goes Here")
。链式方法调用很常见。