斗士狗 2010-07-21 00:41 采纳率: 0%
浏览 370
已采纳

到底什么是 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

  • 写回答

13条回答 默认 最新

  • ?yb? 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).

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站