Java bean的setter是否允许返回? [英] Does Java bean's setter permit return this?

查看:115
本文介绍了Java bean的setter是否允许返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以定义setter方法来返回而不是void吗?

can I define setter method to return this rather than void?

喜欢:

ClassA setItem1() {
      return this;
}

ClassA setItem2() {
      return this;
}

然后我可以使用新的ClassA()。setItem1()。setItem2()

then I can use new ClassA().setItem1().setItem2()

推荐答案

对JavaBeans规范有很多误解。

There is a lot of misunderstanding about JavaBeans spec.

它的存在的主要原因是统一的Java组件模型。这是使用Reflection与Java对象进行编程交互的一种方式。 API本身被命名为 JavaBeans Introspection 。请查看示例用法,您将比一般Java程序员的知识更多。

The main reason for it's existence is the unified Java "component" model. It's a way to interact programatically with a Java Object using Reflection. The API itself is named JavaBeans Introspection. Please, take a look at example usages and You will know a lot more than an average Java programmer does.

内省API可用于统一处理GUI元素。您的组件将其属性公开为一组getter和setter,以便可以在运行时在GUI构建器的属性表上发现和操作。

Introspection API can be used to manipulate GUI elements in an unified manner. Your component exposes it's properties as a pairs of getters and setters so that they could be discovered and manipulated at run-time on a GUI builder's property sheet.

所以,混合流利API和JavaBeans Spec在我看来是一个不用了。这是两个完全不相关的概念,可以互相干扰。当方法签名不同(返回类型)时,JavaBeans内省可能无效。

So, mixing fluent APIs and JavaBeans Spec in my opinion is a no-go. That's two completely unrelated concepts and can disrupt each other. JavaBeans Introspection might not work when method signature differs (return type).

看看这个例子(从链接教程中取出):

Take a look at this example (taken from linked tutorial):

public class SimpleBean
{
private final String name = "SimpleBean";
private int size;

public String getName()
{
    return this.name;
}

public int getSize()
{
    return this.size;
}

public void setSize( int size )
{
    this.size = size;
}

public static void main( String[] args )
        throws IntrospectionException
{
    BeanInfo info = Introspector.getBeanInfo( SimpleBean.class );
    for ( PropertyDescriptor pd : info.getPropertyDescriptors() )
        System.out.println( pd.getName() );
}
}

此示例创建一个非可视化的bean,并显示以下内容从BeanInfo对象派生的属性:

This example creates a non-visual bean and displays following properties derived from the BeanInfo object:


  • class

  • name

  • size

您可能希望看到当您更改 void 返回类型到任何其他。我已经这样做了,结果是一样的。那么这是否意味着它是允许的?

You might want to see what happens when You change void return type to anything else. I have done so and the result is the same. So, does that mean it's allowed?

我害怕没有。 JavaBeans规范对于这些方法签名是非常严格的。刚刚发生的这个实现是宽恕的。尽管如此,我会将流畅的界面与JavaBeans混合使用。你不能真的依靠这个,如果这个发现现在可以工作,那么将来也是如此。

I'm afraid no. The JavaBeans spec is quite strict about those method signatures. It just happened that implementation is forgiving. Nonetheless, I'd disadvise mixing fluent interface with JavaBeans. You can't really rely that, if the discovery works now, it will also in future.

但是,从另一方面看来,你不会使用JavaBeans全面。只有getters / setters对的方法。您应该如何实施和设计您的API。

But, from the other side - it looks like You don't use JavaBeans to full extent. Only the getters/setters pair of method. It's up to You how You implement and design Your APIs.

这篇关于Java bean的setter是否允许返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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