简体   繁体   English

你把这个称作什么?

[英]What do you call this?

Is there a name for this techinique (method calls returning objects upon which another method call is made on the same line)? 该技术是否有名称(方法调用返回对象,并在同一行上对其进行另一个方法调用)?

String pAID = commonAssets.getApplicationServerSettings().getSetting("plivoAuthID");

instead of 代替

ApplicationServerSettings applicationServerSettings = commonAssets.getApplicationServerSettings();
String pAID = applicationServerSettings.getSetting("plivoAuthID");

Also, when I do the first, Eclipse doesn't prompt me to import the class ApplicationServerSettings , but it does if I use the second code style. 另外,当我执行第一个时,Eclipse不会提示我导入类ApplicationServerSettings ,但是如果我使用第二个代码样式,则会提示我。

Also, are these two styles merely preferences? 另外,这两种样式仅仅是偏好吗?

The technique is called method chaining . 该技术称为方法链接

String pAID = commonAssets.getApplicationServerSettings().getSetting("plivoAuthID");

Definition from the wiki: 来自维基的定义:

Method chaining, also known as named parameter idiom, 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.[1] 每个方法都返回一个对象,使调用可以在单个语句中链接在一起,而无需使用变量来存储中间结果。[1] Local variable declarations are syntactic sugar because of the difficulty humans have with deeply nested method calls.[2][3] 局部变量声明是语法糖,因为人类难以深入地嵌套方法调用。[2] [3] A method chain is also known as a train wreck due to the increase in the number of methods that come one after another in the same line that occurs as more methods are chained together[4] even though line breaks are often added between methods. 方法链也被称为火车残骸,原因是在同一行中接连出现的方法数量增加,这是因为将更多方法链接在一起[4],即使方法之间经常添加换行符也是如此。

Your second question: 您的第二个问题:

Also, when I do the first, Eclipse doesn't prompt me to import the class ApplicationServerSettings, but it does if I use the second code style. 同样,当我执行第一个操作时,Eclipse不会提示我导入类ApplicationServerSettings,但是如果我使用第二个代码样式,则会提示我。

  • From the definition again "Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results." 再次从定义中“每个方法都返回一个对象,从而使调用可以在单个语句中链接在一起,而无需使用变量来存储中间结果。” That is why it does not prompt you to import the class ApplicationServerSettings . 这就是为什么它不提示您导入类ApplicationServerSettings

Another example (besides the want you introduce) that looks simpler: 另一个看起来更简单的示例(除了要介绍的示例):

Take a look at the wiki example: 看一下Wiki示例:

class Person {
    private String name;
    private int age;

    // In addition to having the side-effect of setting the attributes in question,
    // the setters return "this" (the current Person object) to allow for further chained method calls.

    public Person setName(String name) {
        this.name = name;
        return this;
    }

    public Person setAge(int age) {
        this.age = age;
        return this;
    }

    public void introduce() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
    }

    // Usage:
    public static void main(String[] args) {
        Person person = new Person();
        // Output: Hello, my name is Peter and I am 21 years old.
        person.setName("Peter").setAge(21).introduce();
    }
}

It's often called fluent syntax . 它通常被称为流利的语法

IMHO it's a matter of style, there's no right or wrong. 恕我直言,这是风格问题,没有对与错。

Fluent syntax is more concise, which is sometimes a good thing. 流利的语法更加简洁,这有时是一件好事。

The other variant is more handy for source-level debugging. 另一个变体对于源代码级调试更方便。 You can step through the statements and inspect the intermediate results. 您可以单步执行这些语句并检查中间结果。

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

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