问题描述
什么是标记接口以及它们用于什么?
What are tagging interfaces and what are they used for?
推荐答案
因为有时候,如果某些属性是真的有意义一个类型可以用作一个类型本身 - Serializable
浮现在脑海中。如果我做这样的方法:
Because sometimes, it really makes sense if some property of a type can be used as a type itself - Serializable
comes to mind. If I make a method like this:
public void save(Object data){ ... }
...你真的不知道数据
将如何保存。 VM序列化? Bean属性序列化?一些自制方案?如果你这样写:
... you don't really know how that data
will be saved. VM serialization? Bean property serialization? Some homebrewed scheme? Whereas if you write it like this:
public void save(Serializable data){ ... }
......很明显(如果只有 ObjectOutputStream的设计者
曾经使用过这种可能性!)。有时,当您想要将元数据添加到类型时使用注释是有意义的,但在这种情况下,我会争论标记接口。
... it is quite clear (if only the designer of ObjectOutputStream
had used this possibility!). Sometimes it makes sense to use annotations when you want to add meta-data to types, but in this case, I'd argue for a tagging interface.
这篇关于用Java标记接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!