【发布时间】:2011-05-31 12:41:52
【问题描述】:
public class UserAction {
private final UUID uuid;
private String userId;
/* more fields, setters and getters here */
public UserAction(){
this.uuid = UUID.fromString(new com.eaio.uuid.UUID().toString());
}
public UserAction(UUID uuid){
this.uuid = uuid;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final UserAction other = (UserAction) obj;
if (this.uuid != other.uuid && (this.uuid == null || !this.uuid.equals(other.uuid))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 53 * hash + (this.uuid != null ? this.uuid.hashCode() : 0);
return hash;
}
}
我正在使用 Gson 来序列化和反序列化这个类。今天我不得不在这个对象中添加一个最终的 UUID。我序列化没有问题。我需要在反序列化时强制 gson 使用 public UserAction(UUID uuid) 构造函数。我怎样才能做到这一点?
【问题讨论】:
标签: java constructor gson deserialization