标准的JavaBean类
①类名:需要见名知义
②成员变量:使用private修饰
③提供至少两个构造方法(无参构造方法,带全部参数的构造方法)
④成员方法:提供每一个成员变量对应的setXXX()和getXXX()方法
如何编写JavaBean类(附注册表单Java代码)
代码如下(示例):
public class User { //Java的注册表单
private String username;
private String password;
private String email;
private String gender;//性别
private int age;
//简便方法1:快捷键(Fn)+alt+insert,然后选择构造函数,按住ctrl+点击想要的属性再确定就能一键生成。set和get方法同理
//简便方法2:插件PTG,右键选择ptg to JavaBean,一键生成构造方法、get/set方法、toString方法和注释
public User() {
}
public User(String username, String password, String email, String gender, int age) {
this.username = username;
this.password = password;
this.email = email;
this.gender = gender;
this.age = age;
}
/**
* 获取
* @return username
*/
public String getUsername() {
return username;
}
/**
* 设置
* @param username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* 获取
* @return password
*/
public String getPassword() {
return password;
}
/**
* 设置
* @param password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* 获取
* @return email
*/
public String getEmail() {
return email;
}
/**
* 设置
* @param email
*/
public void setEmail(String email) {
this.email = email;
}
/**
* 获取
* @return gender
*/
public String getGender() {
return gender;
}
/**
* 设置
* @param gender
*/
public void setGender(String gender) {
this.gender = gender;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
}
以上仅自用,如对您有帮助不胜荣幸