什么是“Java Bean”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1973073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a "Java Bean"?
提问by mrblah
The name really throws me off. I'm hoping someone can explain it in a way I won't forget :)
这个名字真的让我失望。我希望有人能以一种我不会忘记的方式解释它:)
采纳答案by Marc Paradise
Any serializable java class (implementing java.io.Serializable) that follows specific conventions: a no-argument constructor, and properties accessible via get/set/is accessors.
任何遵循特定约定的可序列化 java 类(实现 java.io.Serializable):无参数构造函数和可通过 get/set/is 访问器访问的属性。
The idea is to make it predictable, so that properties etc can be discovered automatically through reflection - of great help in tool and framework development.
这个想法是使其可预测,以便可以通过反射自动发现属性等 - 这对工具和框架开发有很大帮助。
回答by Sampson
http://en.wikipedia.org/wiki/JavaBean
http://en.wikipedia.org/wiki/JavaBean
JavaBeans are reusable software components for Java that can be manipulated visually in a builder tool. Practically, they are classes written in the Java programming language conforming to a particular convention. They are used to encapsulate many objects into a single object (the bean), so that they can be passed around as a single bean object instead of as multiple individual objects. A JavaBean is a Java Object that is serializable, has a nullary constructor, and allows access to properties using getter and setter methods.
JavaBeans 是可重用的 Java 软件组件,可以在构建器工具中进行可视化操作。实际上,它们是用符合特定约定的 Java 编程语言编写的类。它们用于将许多对象封装到单个对象(bean)中,以便它们可以作为单个 bean 对象而不是多个单独的对象传递。JavaBean 是可序列化的 Java 对象,具有空构造函数,并允许使用 getter 和 setter 方法访问属性。
继续阅读»
回答by Matt Phillips
A java bean is a class that is serializable, has a no-argument constructor, and uses getters and setter methods for its member fields. Its used in Java Enterprise Apps to store business logic data.
java bean 是一个可序列化的类,具有无参数构造函数,并为其成员字段使用 getter 和 setter 方法。它在 Java Enterprise Apps 中用于存储业务逻辑数据。
回答by SteJav
Sun'sJavaBean Tutorial says...
The JavaBeans? architecture is based on a component model which enables developers to >create software units called components. Components are self-contained, reusable software units that can be visually assembled into composite components, applets, applications, and servlets using visual application builder tools. JavaBean components are known as beans.
A set of APIs describes a component model for a particular language. The JavaBeans API specificationdescribes the core detailed elaboration for the JavaBeans component architecture.
Beans are dynamic in that they can be changed or customized. Through the design mode of a builder tool you can use the Properties window of the bean to customize the bean and then save (persist) your beans using visual manipulation. You can select a bean from the toolbox, drop it into a form, modify its appearance and behavior, define its interaction with other beans, and combine it and other beans into an applet, application, or a new bean.
JavaBeans?体系结构基于组件模型,该模型使开发人员能够 > 创建称为组件的软件单元。组件是独立的、可重用的软件单元,可以使用可视化应用程序构建器工具可视化地组装成复合组件、小应用程序、应用程序和 servlet。JavaBean 组件称为 bean。
一组 API 描述了特定语言的组件模型。JavaBeans API 规范描述了对 JavaBeans 组件架构的核心详细阐述。
Bean 是动态的,因为它们可以更改或自定义。通过构建器工具的设计模式,您可以使用 bean 的 Properties 窗口来定制 bean,然后使用可视化操作保存(持久化)您的 bean。您可以从工具箱中选择一个 bean,将它放入一个表单中,修改它的外观和行为,定义它与其他 bean 的交互,并将它与其他 bean 组合成一个小程序、应用程序或一个新 bean。
If you've used Swing's 'button', then you've used a component (visible JavaBean). You can use developers tools (like NetbeansIDE) to change the Bean's available 'properties'. Netbeans uses something called 'introspection' to discover which JavaBean propertiescan be modified by the coder/user (e.g. name, text-titleand alignmentfor a Swing Button JavaBean component). You can save its state too (the IDE/Beans developer might use 'serialization' to do this) allowing re-use with your favourite settings another time.
如果您使用过 Swing 的“按钮”,那么您就使用了一个组件(可见的 JavaBean)。您可以使用开发人员工具(如 NetbeansIDE)来更改 Bean 的可用“属性”。Netbeans 使用一种称为“内省”的方法来发现编码人员/用户可以修改哪些 JavaBean属性(例如Swing Button JavaBean 组件的名称、文本标题和对齐方式)。您也可以保存它的状态(IDE/Beans 开发人员可能会使用“序列化”来执行此操作),以便下次使用您喜欢的设置重新使用。
JavaBeans don't need to be visible (like a swing component). You could create your own JavaBean to encrypt text in a textbox when someone clicks an 'OK' button on a form. You don't see your custom written JavaBean, but some other developer could re-use your 'encryption' JavaBean in their code with some 'property' changes that you allowed to be public (i.e. encryption-type="blowfish").
JavaBeans 不需要是可见的(就像一个swing 组件)。当有人单击表单上的“确定”按钮时,您可以创建自己的 JavaBean 来加密文本框中的文本。您看不到自定义编写的 JavaBean,但其他一些开发人员可以在他们的代码中重新使用您的“加密”JavaBean,并进行一些您允许公开的“属性”更改(即加密类型 =“blowfish”)。
Regards, SteJav
问候, SteJav
回答by akashchandrakar
JavaBeans are reusable software component written in java.The components can be configured and connected using builder tools.Three key properties that causes any class in java to become a javabean is
JavaBeans 是用 java 编写的可重用软件组件。这些组件可以使用构建器工具进行配置和连接。导致 java 中的任何类成为 javabean 的三个关键属性是
1.Class is serializable
2.class has a 0 argument constructor
3.class has getter and setter methods for data members
Here is a simple class that is eligible for becoming a javabean
这是一个有资格成为javabean的简单类
import java.io.*;
public class Student implements Serializable {
private String name = null;
//0 argument constructor
public Student() {
}
//getter method
public String getName() {
return name;
}
//settor method
public void setName(final String name) {
this.name = value;
}
}
回答by satender
A JavaBean is a Java object that satisfies below programming conventions:
JavaBean 是满足以下编程约定的 Java 对象:
- The JavaBean class must implement Serializable Interface
- The JavaBean class must have a no-arg constructor which should be public
- All JavaBean properties must have public Setter and Getter methods to set and get all bean properties.
- All JavaBean instance variables should be private and only accessible by Getter and setter.
- JavaBean 类必须实现 Serializable 接口
- JavaBean 类必须有一个应该是公共的无参数构造函数
- 所有 JavaBean 属性都必须具有公共 Setter 和 Getter 方法来设置和获取所有 bean 属性。
- 所有 JavaBean 实例变量都应该是私有的,并且只能由 Getter 和 setter 访问。