首页 新闻 赞助 找找看

红色的代码有什么用??是什么意思??求大神赐教!!

0
[已解决问题] 解决于 2016-12-20 21:48

public class UserBean {
private int id;
private String userName;
private String trueName;
private String sex;
private String birthday;
private String idCard;
private String phone;
private String address;
private String email;
private String userType;
private String password;

public UserBean() {
}
public UserBean(String userName, String trueName, String sex,
String birthday, String idCard, String phone, String address,
String email, String userType, String password) {
this.userName = userName;
this.trueName = trueName;
this.sex = sex;
this.birthday = birthday;
this.idCard = idCard;
this.phone = phone;
this.address = address;
this.email = email;
this.userType = userType;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getTrueName() {
return trueName;
}
public void setTrueName(String trueName) {
this.trueName = trueName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "userBean [id=" + id + ", userName=" + userName + ", trueName="
+ trueName + ", sex=" + sex + ", birthday=" + birthday
+ ", idCard=" + idCard + ", phone=" + phone + ", address="
+ address + ", email=" + email + ", userType=" + userType
+ ", password=" + password + "]";
}


}

宇随心的主页 宇随心 | 菜鸟二级 | 园豆:202
提问于:2016-12-19 20:47
< >
分享
最佳答案
0
public UserBean() {
}

无参构造,就是说可以 UserBean user = new UserBean(); 这样的初始化对象.只要没有定义构造器,默认是无参构造.如果定义带参构造没有定义无参构造,那么只能通过带参的构造进行初始化.

public UserBean(String userName, String trueName, String sex,
String birthday, String idCard, String phone, String address,
String email, String userType, String password) {
this.userName = userName;
this.trueName = trueName;
this.sex = sex;
this.birthday = birthday;
this.idCard = idCard;
this.phone = phone;
this.address = address;
this.email = email;
this.userType = userType;
this.password = password;
}

带参的构造器,可以通过UserBean user = new UserBean(“userName,trueName……此处代码省略”);参数顺序和类型要和定义的时候一致.

@Override
public String toString() {
return "userBean [id=" + id + ", userName=" + userName + ", trueName="
+ trueName + ", sex=" + sex + ", birthday=" + birthday
+ ", idCard=" + idCard + ", phone=" + phone + ", address="
+ address + ", email=" + email + ", userType=" + userType
+ ", password=" + password + "]";
}

这里的toString(),是有@Override注解的,说明是重写父类的toString()方法, 这里的父类是指的Object类.

奖励园豆:5
、熙和 | 小虾三级 |园豆:1508 | 2016-12-20 11:41
其他回答(1)
0

1. 第一处红色地方是构造器,也就是使用new关键字是传进去的初始化值

2.实现Object里面toString()方法,在使用System.out.println(UserBean对象)的时候会自动调用toString()方法

其实就是一个出生就有的方法而已。默认是有实现的。

jasondyoung | 园豆:424 (菜鸟二级) | 2016-12-19 22:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册