Java Bean将许多对象封装为一个,如何? [英] Java Bean encapsulates many objects into one,How?

查看:272
本文介绍了Java Bean将许多对象封装为一个,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始阅读有关Java Bean的文章,我有一个与这个

I'm newly started reading about Java Beans and I had a question which was exactly same as this Topic's question. So I repeat The question:

在定义上说" java bean将许多对象封装到一个对象(bean)中."

1.许多对象"是什么意思?

1.What does "many objects" here mean?

2.java Bean如何将它们封装到一个对象中?

2.How they are encapsulated into one object by java beans?


来自 Java Beans Wikipedia :

在基于Java平台的计算中,JavaBeans是将许多对象封装到单个对象(bean)中的类.

in computing based on the Java Platform, JavaBeans are classes that encapsulate many objects into a single object (the bean).


Edit2: 所有类都具有具有多个属性和字段的能力. 如果封装许多对象意味着具有多个属性和字段,我不明白为什么它们提到此功能是Java bean类的优势.


all of classes have ability of having multiple attributes and fields. If encapsulating of many objects means having multiple attributes and fields, I don't understand why they mentioned to this ability as a advantage of java bean class.

推荐答案

首先要弄清楚,Java中的每个Class都扩展了Object类型. String之类的东西也是Object.

First to make it clear, every Class in Java extends the type Object. Something like String is also an Object.

许多对象"是指我们如何将不同的对象用作Bean中的字段.这将与Bean和Objects建立关系.

The "many objects" is referring to how we can use different objects as fields within the bean. This creates a has-a relationship with the bean to your Objects.

例如,说我们有这个Bean:

public class YourBean implements java.io.Serializable {

    private String s;
    private ArrayList<String> list;

    //Omitted rest of bean boilerplate
}

此示例将在其中包含两个不同的Object,分别为String s和名为listArrayList<String>.您可以根据需要向bean中添加任意数量的Objects和基元.

This example will contain two different Objects inside of it, the String s and the ArrayList<String> named list. You can add as many different Objects and primitives to your bean as you want.

要使用no-args构造函数创建bean,则应使用:

To create the bean with a no-args constructor, you would then use:

YourBean bean = new YourBean();

您可以设置并获取封装在其中的Objects的值:

And you can set and get the values of the Objects encapsulated within with:

ArrayList<String> yourList = new ArrayList<>();
bean.setList(yourList);
System.out.println(bean.getList());

通过引用我命名为bean的bean Object,您将能够以此方式引用bean中的所有Objects.

You will be able to refer to all the Objects inside the bean this way by referencing the bean Object I named bean.

此外,您还可以创建多个相同类型的bean,因此每次创建new YourBean()时,您也将能够使用其中包含的所有Objects.

Additionally, you can create multiple of the same type of bean as well, so every time you make a new YourBean(), you will also be able to use all the Objects contained within.

此功能不是Bean所独有的,您可以在任何Class中进行此操作,而Bean是一个术语,用于描述编写某些类的特定方式.

This functionality is not unique to a Bean, you can do this in any Class, rather a Bean is a term used to describe a specific way you write some classes.

我建议研究 Java组合,以了解何时应使用具有关系,而不是使用继承( >是关系.

I recommend looking into Java Composition to learn when you should use a has-a relationship, rather than inheritance which is an is-a relationship.

这篇关于Java Bean将许多对象封装为一个,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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