方法在Java中链接 [英] Method Chaining in Java

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

问题描述

虽然我早些时候回答了一些问题并且最近一直在做的一些工作,但我一直想知道为什么Java不支持内置类的方法链接。



<例如,如果我要创建一个 Car 类,我可以通过重新调整来使可链接 c $ c>而不是如下所示:

  public class Car {
private String make;

public Car setMake(String make){
this.make = make;
返回此;
}
}

内置图书馆有什么特别的理由不要倾向于这样做吗?方法链是否存在缺点?



我可能忽略了一些可以解释缺少方法链接的东西,但是任何默认返回void的setter方法都应该返回一个引用 this (至少在我看来应该如此)。这会使下面的情况更加清晰。

  container.add((新的JLabel(标签文本))。setMaximumSize (new Dimension(100,200))); 

而不是更长的啰嗦:注意:它不会阻止你这样编码如果你愿意的话。

  JLabel label = new JLabel(label text); 
label.setMaximumSize(new Dimension(100,200));
container.add(label);

我很想知道这个决定背后的原因,如果我不得不猜测它会是因此存在与此相关的开销,因此只应在需要时使用。

解决方案

呃。可以在两个方向上进行可读性论证 - 这样的事情就是试图将过多的东西放到一行中。



但老实说,我怀疑这是历史的原因:普遍的链接行为并没有真正成为流行或众所周知的例如Swing正在开发中。您可能会争辩说它应该在以后添加,但是这样的事情往往会产生二进制不兼容性以及Sun / Oracle历来非常谨慎的其他问题。



更新的JDK库 - 参见例如 ByteBuffer 对于一个主要的,众所周知的例子 - 提供链接行为等,这是有意义的。


While answering a few questions on here earlier and from some work I have been doing lately I have been wondering why Java does not support method chaining on its built in classes.

If I were to create a Car class for example, I could make it chainable by reutrning this instead of void as follows:

public class Car {
    private String make;        

    public Car setMake(String make) {
        this.make = make;
        return this;
    }   
}

Is there any particular reason why the built in libraries don't tend to do things this way? Is there a downside to method chaining?

I may have overlooked something which would explain the lack of method chaining however any setter method that returns void by default should return a reference to this (at least in my eyes it should). This would make situations like the following much cleaner.

container.add((new JLabel("label text")).setMaximumSize(new Dimension(100,200)));

rather than the more long winded: Note: It would not stop you from coding this way if you wished.

JLabel label = new JLabel("label text");
label.setMaximumSize(new Dimension(100,200));
container.add(label);

I would be very interested to hear the reasons behind this decision, If I had to guess it would be that there is an overhead associated with this and so should only be used when needed.

解决方案

Eh. There's readability arguments to be made in both directions -- there's such a thing as trying to put too much into a single line.

But honestly, I suspect here it's for historical reasons: pervasive "chaining" behavior hadn't really become popular or well-known when e.g. Swing was being developed. You could argue that it should've been added in later on, but things like that tend to create binary incompatibilities and other issues that Sun/Oracle have historically been extremely cautious about.

More recent JDK libraries -- see e.g. ByteBuffer for a major, well-known example -- have provided chaining behavior and the like, where it makes sense.

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

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