Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
由于 Serializable 和 Cloneable 是标记接口(没有任何方法的接口),所以如果我们创建自己的标记接口,那么这两者之间会有什么区别。
Serializable并且Cloneable是标准接口。它们都属于“受保护”的包(这意味着你不能向它们添加类——你可以,但这不是你每天都做的事情)。
Serializable
Cloneable
java.io.Serializable java.lang.Cloneable
它们在运行时被 JVM 识别。 例如调用clone()一个没有实现的对象Cloneable会抛出一个
clone()
java.lang.CloneNotSupportedException
您可能希望将标记接口用于自定义运行时检查,甚至用于编译时处理。但是推荐的方法是使用注释。