妄徒之命 2010-07-21 00:41 采纳率: 100%
浏览 584
已采纳

到底什么是 JavaBean?

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?

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?

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.

转载于:https://stackoverflow.com/questions/3295496/what-is-a-javabean-exactly

  • 写回答

14条回答 默认 最新

  • larry*wei 2010-07-21 00:45
    关注

    A JavaBean is just a standard

    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.

    With respect to Serializable, from the API documentation:

    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.

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

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(13条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制