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

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

问题描述

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

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

喜欢:

ClassA setItem1() {
      return this;
}

ClassA setItem2() {
      return this;
}

然后我可以使用 new ClassA().setItem1().setItem2()

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

推荐答案

关于 JavaBeans 规范存在很多误解.

There is a lot of misunderstanding about JavaBeans spec.

它存在的主要原因是统一的Java组件"模型.这是一种使用反射以编程方式与 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.

因此,在我看来,混合 fluent 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:

  • 班级
  • 姓名
  • 尺寸

您可能想看看当您将 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 规范对这些方法签名非常严格.只是碰巧实施是宽容的.尽管如此,我不建议将 fluent interface 与 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.只有 getter/setter 方法对.实现和设计 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 permit 是否返回这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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