Appearance
JavaBeans
In Java, there are many class
definitions that conform to this specification:
- Several
private
instance fields; - Read and write instance fields through
public
methods.
For example:
java
public class Person {
private String name;
private int age;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
public int getAge() { return this.age; }
public void setAge(int age) { this.age = age; }
}
If the read and write methods conform to the following naming convention:
java
// Reading method:
public Type getXyz()
// write method:
public void setXyz(Type value)
Then this class
is called JavaBean
:
The above field is xyz
, then the read and write method names start with get
and set
respectively, and are followed by the field name Xyz
starting with an uppercase letter, so the two read and write method names are getXyz()
and setXyz()
respectively.
The boolean
field is special, and its reading method is generally named isXyz()
:
java
// Reading method:
public boolean isChild()
// write method:
public void setChild(boolean value)
We usually refer to a set of corresponding read methods ( getter
) and write methods ( setter
) as properties ( property
). For example, the name
attribute:
- The corresponding reading method is
String getName()
- The corresponding writing method is
setName(String)
Properties with only getter
are called read-only properties. For example, define an age read-only property:
- The corresponding reading method is
int getAge()
- There is no corresponding writing method
setAge(int)
Similarly, properties with only setter
are called write-only properties.
Obviously, read-only properties are common and write-only properties are uncommon.
Properties only need to define getter
and setter
methods, and do not necessarily need corresponding fields. For example, child
read-only property is defined as follows:
java
public class Person {
private String name;
private int age;
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
public int getAge() { return this.age; }
public void setAge(int age) { this.age = age; }
public boolean isChild() {
return age <= 6;
}
}
It can be seen that getter and setter are also a method of data encapsulation.
The role of JavaBeans
JavaBean is mainly used to transfer data, that is, to combine a set of data into a JavaBean for easy transmission. In addition, JavaBean can be easily analyzed by IDE tools to generate code for reading and writing properties, which is mainly used in the visual design of graphical interfaces.
Through the IDE, getter
and setter
can be quickly generated. For example, in Eclipse, first enter the following code:
java
public class Person {
private String name;
private int age;
}
Then, right-click, select "Source", "Generate Getters and Setters" in the pop-up menu, select the fields that need to generate getter
and setter
methods in the pop-up dialog box, click OK, and the IDE will automatically complete all method codes.
Enumeration JavaBean properties
To enumerate all properties of a JavaBean, you can directly use Introspector
provided by the Java core library:
java
import java.beans.*;
public class Main {
public static void main(String[] args) throws Exception {
BeanInfo info = Introspector.getBeanInfo(Person.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
System.out.println(pd.getName());
System.out.println(" " + pd.getReadMethod());
System.out.println(" " + pd.getWriteMethod());
}
}
}
class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Running the above code will list all attributes and corresponding read and write methods. Note that the class
attribute is brought by the getClass()
method inherited from Object
.
Summary
JavaBean is a class
that conforms to naming conventions and defines properties through getter
and setter
;
Attribute is a common name and is not specified by Java grammar;
You can use IDE to quickly generate getter
and setter
;
Use Introspector.getBeanInfo()
to get the property list.