Fields in a "Serializable" class should either be transient or serializable

Fields in a “Serializable” class should either be transient or serializable
修改质检问题时遇到这种

Noncompliant Code Example
public class Address {
//…
}

public class Person implements Serializable {
private static final long serialVersionUID = 1905122041950251207L;

private String name;
private Address address; // Noncompliant; Address isn’t serializable
}
Compliant Solution
public class Address implements Serializable {
private static final long serialVersionUID = 2405172041950251807L;
}

public class Person implements Serializable {
private static final long serialVersionUID = 1905122041950251207L;

private String name;
private Address address;
}

对于代码中属性是集合接口的这些不需要实例化
private List sellst;

将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会被序列化。

你可能感兴趣的:(sonar质检)