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

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

问题描述

我认为,我认为Bean是一个带有属性和getter / setter的Java类。据我所知,它相当于C结构。这是真的吗?

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?

此外,bean和普通类之间是否存在真正的语法差异?是否有任何特殊定义或界面?

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

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

Basically, why is there a term for this?

编辑:如果您能够如此友善并添加有关 Serializable 界面的信息,以及它的含义,请回答,我将非常感激。

Edit: If you can be so kind and add information regarding the Serializable interface, and what it means, to your answer, I'd be very grateful.

推荐答案

JavaBean只是一个标准

A JavaBean is just a standard


  1. 所有属性私有(使用 getters / setters

  2. 公众无参数构造函数

  3. 实现 Serializable

  1. All properties private (use getters/setters)
  2. A public no-argument constructor
  3. Implements Serializable.

就是这样。这只是一个惯例。很多库都依赖于它。

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 defines a JavaBean if it follows the standards.

有一个术语,因为标准允许库以编程方式对您以预定义方式定义的类实例执行操作。例如,如果一个库想要传递你传递给它的任何对象,它就知道它可以因为你的对象是可序列化的(假设lib要求你的对象是正确的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 lib requires your objects be proper JavaBeans).

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

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