问题描述
当Serializable
之类的标记接口没有什么可实现的时候,实现它有什么用?
When there is nothing to implement in the marker interfaces like Serializable
What is the use of implementing it?
推荐答案
在 Java 的早期版本中,标记接口 是声明类元数据的唯一方法.例如,Serializable Marker Interface 让类的作者说他们的类在序列化和反序列化时会正确运行.
In earlier versions of Java, Marker Interfaces were the only way to declare metadata about a class. For example, the Serializable Marker Interface lets the author of a class say that their class will behave correctly when serialized and deserialized.
在现代 Java 中,标记接口没有位置.它们可以完全替换为 Annotations,其中允许非常灵活的元数据功能.如果您有关于某个类的信息,并且该信息永远不会改变,那么注释是一种非常有用的表示方式.
In modern Java, marker interfaces have no place. They can be completely replaced by Annotations, which allow for a very flexible metadata capability. If you have information about a class, and that information never changes, then annotations are a very useful way to represent it.
这篇关于Java中标记接口的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!