今天在写mybatis相关代码时,在创建了两个类,一个account类,另一个是accountUser类作为子类
代码如下
account类作为父类
package com.domain;
import java.io.Serializable;
public class Account implements Serializable {
private Integer ID;
private Integer UID;
private Double MONEY;
public Integer getID() {
return ID;
}
public void setID(Integer ID) {
this.ID = ID;
}
public Integer getUID() {
return UID;
}
public void setUID(Integer UID) {
this.UID = UID;
}
public Double getMONEY() {
return MONEY;
}
public void setMONEY(Double MONEY) {
this.MONEY = MONEY;
}
@Override
public String toString() {
return "Account{" +
"ID=" + ID +
", UID=" + UID +
", MONEY=" + MONEY +
'}';
}
}
AccountUser 作为子类
package com.domain;
import java.io.Serializable;
public class AccountUser extends Account implements Serializable{
private String username;
private String address;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//这里不能用这种方法
// @Override
// public String toString() {
// return "AccountUser{" +
// "username='" + username + '\'' +
// ", address='" + address + '\'' +
// '}';
// }
//要加上父类的tostring方法
public String toString() {
return super.toString()
+"AccountUser{" +
"username='" + username + '\'' +
", address='" + address + '\'' +
'}';
}
}
但是在测试时发现
打印不出父类的信息,发现是自己的子类的tostring方法写的有问题
要写成下面,先继承父类的tostring方法
//要加上父类的tostring方法
public String toString() {
return super.toString() + " AccountUser [username=" + username + ",address=" + address + "]";
}
这样就好了
新创建一个公众号 Rockey小何同学 想相互交流的同学可以关注一下哈! 感谢支持!