java 在java中将子类设为不可序列化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2229033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:10:23  来源:igfitidea点击:

Making Child classes as Non Serializable in java

javainheritanceoopserialization

提问by GuruKulki

I have a class which implements Serializable. Now I extends this class and I want this extended class to be non Serializable. So how to do it?

我有一个实现可序列化的类。现在我扩展了这个类,我希望这个扩展类是不可序列化的。那么怎么做呢?

For example. I have

例如。我有

class A implements Serializable.

and I have

我有

class B extends A.

But I want class B to be Non Serializable.

但我希望 B 类是不可序列化的。

采纳答案by Hyman Leow

As others have made clear, it's not possible for a subclass of a Serializableclass to be non-Serializable.

正如其他人已经明确指出的那样,类的子Serializable类不可能是非Serializable.

If what you want is for the subclass' attributes not to be serialized, one option is to make them all transient.

如果您想要的是不序列化子类的属性,一种选择是使它们全部为瞬态。

If you need more than that (you don't want super class fields to be serialized), override writeObject(ObjectOutputStream)and readObject(ObjectInputStream)as outlined here - https://web.archive.org/web/20120626144013/http://java.sun.com/developer/technicalArticles/ALT/serialization

如果您需要更多(您不希望超类字段被序列化),请覆盖writeObject(ObjectOutputStream)readObject(ObjectInputStream)按照此处概述 - https://web.archive.org/web/20120626144013/http://java.sun.com/开发人员/技术文章/ALT/序列化

回答by finnw

You can't remove the interface, but you can prevent serialization at run-time:

您不能删除接口,但可以在运行时防止序列化:

class B extends A {
    private void writeObject(ObjectOutputStream oos) throws IOException {
        throw new NotSerializableException();
    }
}

回答by Michael Borgwardt

Not possible. Liskov Substitution Principleand all.

不可能。Liskov 替换原则等等。

回答by BalusC

That's indeed not possible. Your best bet is to let B composite/decorate A.

那确实是不可能的。最好的办法是让 B 合成/装饰 A。

public class B {
    private A a = new A();

    public void doSomething() {
        a.doSomething();
    }
}

回答by Cuga

It will always be Serializable, though you could ensure nothing in the class ever gets serialized by making every member transient.

它始终是可序列化的,尽管您可以通过使每个 member 来确保类中的任何内容都不会被序列化transient

回答by Pascal Thivent

Answering one of your comment (and assuming you are talking of JPA or Hibernate):

回答您的评论之一(并假设您在谈论 JPA 或 Hibernate):

But Any class which wants its object to be persisted should implement this. and I want my Class B's objects as non persistable.

但是任何想要持久化其对象的类都应该实现这一点。我希望我的 B 类对象不可持久化。

If you don't want B to be persistent at all, don't mark it as @Entity. But it doesn't really make sense for B to extend A if B is not a persistent entity IMHO. This would be a bad use of inheritance.

如果您根本不希望 B 持久化,请不要将其标记为@Entity. 但是,恕我直言,如果 B 不是持久实体,那么 B 扩展 A 并没有真正意义。这将是对继承的不良使用。

回答by Venkata

Need to implememnt readObject() and writeObject() Methods

需要实现 readObject() 和 writeObject() 方法