什么是 Java 豆?
可能重复:
豆子有什么意义?
什么是爪哇豆?它的用途是什么?有哪些代码示例?我听说它用于与getter和setter方法有关的事情?我对Java Bean是什么以及您甚至在哪里访问它感到非常困惑。我用谷歌搜索了它,但找不到明确的答案。
可能重复:
豆子有什么意义?
什么是爪哇豆?它的用途是什么?有哪些代码示例?我听说它用于与getter和setter方法有关的事情?我对Java Bean是什么以及您甚至在哪里访问它感到非常困惑。我用谷歌搜索了它,但找不到明确的答案。
Java Bean 是一个普通的 Java 类,它具有私有属性及其公共 getter 和 setter 方法。
Java Bean通常用作帮助器类。
示例 -
public class User implements java.io.Serializable {
private String name;
private Integer age;
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public Integer getAge(){
return this.age;
}
public void setAge(Integer age){
this.age = age;
}
}
实现不是强制性的,但如果你想在Java的内存之外持久化或传输Javabean,例如在硬盘中或通过网络,实现是非常有用的。Serializable
JavaBeans是Java的可重用软件组件。实际上,它们是用Java编程语言编写的符合特定约定的类。它们用于将许多对象封装到单个对象(bean)中,以便它们可以作为单个bean对象而不是作为多个单独的对象传递。JavaBean 是一个可序列化的 Java 对象,具有 0 参数构造函数,并允许使用 getter 和 setter 方法访问属性。
优势
弊
例:
package beans;
/**
* Class <code>PersonBean</code>.
*/
public class PersonBean implements java.io.Serializable {
private String name;
private boolean deceased;
static final long serialVersionUID = 1L;
/** No-arg constructor (takes no arguments). */
public PersonBean() {
}
/**
* Property <code>name</code> (note capitalization) readable/writable.
*/
public String getName() {
return this.name;
}
/**
* Setter for property <code>name</code>.
* @param name
*/
public void setName(final String name) {
this.name = name;
}
/**
* Getter for property "deceased"
* Different syntax for a boolean field (is vs. get)
*/
public boolean isDeceased() {
return this.deceased;
}
/**
* Setter for property <code>deceased</code>.
* @param deceased
*/
public void setDeceased(final boolean deceased) {
this.deceased = deceased;
}
}
参考 http://en.wikipedia.org/wiki/JavaBeans
根据@Andy的评论,我接受我们应该声明serialVersionUID。根据 Java 文档
If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class--serialVersionUID fields are not useful as inherited members. Array classes cannot declare an explicit serialVersionUID, so they always have the default computed value, but the requirement for matching serialVersionUID values is waived for array classes.
如果您没有显式指定 serialVersionUID,则会自动生成一个值 - 但这是脆弱的,因为它依赖于编译器实现。