不可序列化异常,尽管所有字段都是可序列化的

huangapple go评论65阅读模式
英文:

Not serializable exception even though all fields are serializable

问题

我有以下类定义,我想将其保存在文件中,它的所有字段都是可序列化的,那么为什么这个类不是可序列化的?

我在其他回答中看到不需要使用getter和setter,所以我已经在类定义中省略了它们。

private class MyClass implements Serializable
{
    private static final long serialVersionUID = 314L;

    public HashMap<Long, String> allTags = new HashMap<Long, String>();
    public HashMap<String, String> notes = new HashMap<String, String>();
    public HashMap<String, ArrayList<Long>> tags = 
                                   new HashMap<String, ArrayList<Long>>();
}
英文:

I have the following class definition which I want to save in a file, and all its fields are serializable, so why isn't the class serializable?

I've read in other responses that I don't need getters and setters, so I've omitted them from the class definition.

private class MyClass implements Serializable
{
	private static final long serialVersionUID = 314L;

	public HashMap&lt;Long, String&gt; allTags = new HashMap&lt;Long, String&gt;();
	public HashMap&lt;String, String&gt; notes = new HashMap&lt;String, String&gt;();
	public HashMap&lt;String, ArrayList&lt;Long&gt;&gt; tags = 
                                   new HashMap&lt;String, ArrayList&lt;Long&gt;&gt;();
}

答案1

得分: 2

直截了当地,封装的类需要是Serializable

英文:

Straight forward, the enclosing class needs to be Serializable.

答案2

得分: 0

如果这是一个内部类,请使外部类可序列化,并序列化外部类实例。

英文:

If this is an inner class, make the outer class serializable and serialzie outer class instance

huangapple
  • 本文由 发表于 2020年10月19日 13:32:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64421643.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定