究竟什么是 JavaBean? [英] What is a JavaBean exactly?

查看:53
本文介绍了究竟什么是 JavaBean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解,我认为,一个Bean"是一个带有属性和 getter/setter 的 Java 类.
据我所知,它相当于一个 C struct.是真的吗?

I understood, I think, that a "Bean" is a Java-class with properties and getters/setters.
As much as I understand, it is the equivalent of a C struct. Is that true?

此外,JavaBean 和常规的 class 之间是否存在真正的语法差异?
有没有什么特殊的定义或者Interface?

Also, is there a real syntactic difference between a JavaBean and a regular class?
Is there any special definition or an Interface?

基本上,为什么会有这样的术语?

Basically, why is there a term for this?

还有 Serializable 接口是什么意思?

Also what does the Serializable interface mean?

推荐答案

JavaBean 只是一个 标准

A JavaBean is just a standard

  1. 所有属性都是私有的(使用getters/setters)
  2. 公共无参数构造函数
  3. 实现可序列化.

就是这样.这只是一个约定.不过,很多库都依赖它.

That's it. It's just a convention. Lots of libraries depend on it though.

关于 Serializable,来自 API 文档:

类的可序列化由实现类的类启用java.io.Serializable 接口.不实现这个的类接口将不会对其任何状态进行序列化或反序列化.可序列化类的所有子类型本身都是可序列化的.这序列化接口没有方法或字段,仅用于识别可序列化的语义.

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.

换句话说,可序列化的对象可以写入流,因此可以写入文件、对象数据库等.

In other words, serializable objects can be written to streams, and hence files, object databases, anything really.

此外,JavaBean 和另一个类在语法上没有区别——如果一个类遵循标准,那么它就是一个 JavaBean.

Also, there is no syntactic difference between a JavaBean and another class -- a class is a JavaBean if it follows the standards.

它有一个术语,因为该标准允许库以编程方式使用您以预定义方式定义的类实例进行操作.例如,如果一个库想要流式传输你传递给它的任何对象,它知道它可以,因为你的对象是可序列化的(假设库要求你的对象是正确的 JavaBeans).

There is a term for it, because the standard allows libraries to programmatically do things with class instances you define in a predefined way. For example, if a library wants to stream any object you pass into it, it knows it can because your object is serializable (assuming the library requires your objects be proper JavaBeans).

这篇关于究竟什么是 JavaBean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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