JavaBeans: Difference between revisions

Content deleted Content added
m +pt:IW
Added boolean field example
Line 17:
 
== JavaBean Example ==
<pre>
public class PersonBean implements java.io.Serializable {
private String name;
private intString agename;<BR>
private Stringint nameage;
public PersonBean() {
private boolean alive;
// Our constructor. Note that it takes no arguments.
}<BR>
public void setName(String n) {
this.name = n;
}<BR>
public void setAge(int a) {
this.age = a;
} <BR>
public String getName() { return (this.name); }
public int getAge() { return (this.age); }
}
 
PersonBean person = new public PersonBean(); {
// Our constructor. Note that it takes no arguments (a default constructor).
person.setName("Bob");
}<BR>
System.out.println(person.getName());
 
public void setName(String n) {
this.name = n;
}<BR>
 
public PersonBeanvoid setAge(int a) {
this.age = a;
}
 
public void setAgesetDeceased(intboolean adeceased) {
this.deceased = deceased;
}
 
public String getName() { return (this.name); }
public int getAge() { return (this.age); }
// note slightly different semantics for a boolean field (*is* vs. *get*)
public boolean isDeceased() { return (this.deceased); }
}
 
PersonBean person = new PersonBean();
person.setName("Bob");
person.setAlive(false);
System.out.println(person.getName() + (person.isDeceased() ? " [deceased]");
</pre>
 
== See also ==