Java中标记接口的用途是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1995198/
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
What is the use of marker interfaces in Java?
提问by GuruKulki
When there is nothing to implement in the marker interfaces like Serializable
What is the use of implementing it?
当标记接口中没有什么要实现Serializable
的时候,实现它有什么用?
采纳答案by Chris Pitman
In earlier versions of Java, Marker Interfaceswere 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 的早期版本中,标记接口是声明类元数据的唯一方法。例如,Serializable Marker Interface 让类的作者说他们的类在序列化和反序列化时会正确运行。
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 中,标记接口没有位置。它们可以完全被Annotations取代,这允许非常灵活的元数据功能。如果你有关于一个类的信息,而且这些信息永远不会改变,那么注释是一种非常有用的表示它的方式。
回答by Brian Agnew
It indicates that the class (and consequently all the fields which aren't transient) are candidates for serialisation. And if you're building a framework reliant on serialisation, you can of course write a method thus:
它表明该类(以及所有非瞬态字段)是序列化的候选者。如果您正在构建一个依赖于序列化的框架,您当然可以这样编写一个方法:
public void registerObject(Serializable obj);
to limit the classes you're prepared to accept.
限制您准备接受的课程。
Because a serialized object needs to retain compatibility across systems, serialisation is an explicit design decisionand hence requires the use of the marker interface, to identify such candidates.
因为序列化对象需要保持跨系统的兼容性,序列化是一个明确的设计决策,因此需要使用标记接口来识别这些候选对象。
There's also a security aspect. You don't want to make everything serialisable - otherwise you can accidentally expose (say) passwords or other sensitive data via serialisation.
还有一个安全方面。您不想使所有内容都可序列化 - 否则您可能会通过序列化意外暴露(例如)密码或其他敏感数据。
回答by Gregory Pakosz
Such marker interfaces are useful in the case other code takes decisions depending on whether an object implements some marker interface.
在其他代码根据对象是否实现某些标记接口来做出决定的情况下,此类标记接口很有用。
In the case of Serializable
, reflection will be used to serialize the fields of the objects.
在 的情况下Serializable
,反射将用于序列化对象的字段。
Now annotations are preferred as they don't propagate to sub-classes.
现在首选注释,因为它们不会传播到子类。
请参阅标记接口模式。
回答by Bozho
They are called markerinterfaces. And as the name implies, they markthat some object is available for certain sort of operations.
它们被称为标记接口。顾名思义,它们标志着某些对象可用于某些类型的操作。
Serializable
means that the object is eligible for java serialization, for example.
Serializable
例如,表示该对象有资格进行 java 序列化。
It has been discussed whether they shouldn't be replaced by annotations, since their functions are quite similar.
已经讨论过它们是否不应该被注解取代,因为它们的功能非常相似。
回答by cyborg
If you implement an interface then instanceof
will be true. If you interface has nothing to implement then you can use this to mark a class with meta-data like annotations do for Java 1.5 and up without having to force the implementor to do anything special.
如果你实现一个接口,那么instanceof
将是真的。如果您的接口没有要实现的内容,那么您可以使用它来标记具有元数据的类,就像 Java 1.5 及更高版本的注释一样,而不必强制实现者做任何特殊的事情。
回答by Itay Maman
You are right in reasoning that an empty interface does not affect the "standard" execution of the program which is based on inspection/mutation of fields and dispatching of methods.
您认为空接口不会影响基于字段检查/变异和方法分派的程序的“标准”执行是正确的。
However, marker interface are useful when used in conjunction with reflection: A library/method inspects (via reflection) an object and works differently if its class impplements the marker interface. As of Java5 there's very little need for marker interfaces - the same "marking" facility can be achieved via Java annotations - which (again) most of their effect will be achieved via reflection-based code.
然而,当与反射结合使用时,标记接口很有用:库/方法检查(通过反射)一个对象,如果它的类实现了标记接口,则其工作方式会有所不同。从 Java5 开始,几乎不需要标记接口——同样的“标记”工具可以通过 Java 注释来实现——(再次)它们的大部分效果将通过基于反射的代码实现。
回答by T C
Joshua Bloch: Effective Java 2nd Edition, p 179
Joshua Bloch: Effective Java 第二版,第 179 页
Item 37: Use marker interfaces to define types
... You may hear it said that marker annotations (Item 35) make marker interfaces obsolete. This assertion is incorrect. Marker interfaces have two advantages over marker annotations. First and foremost, marker interfaces define a type that is implemented by instances of the marked class; marker annotations do not. The existence of this type allows you to catch errors at compile time that you couldn't catch until runtime if you used a marker annotation....
第 37 条:使用标记接口定义类型
... 您可能听说过标记注释(第 35 项)使标记接口过时。这种说法是不正确的。与标记注释相比,标记接口有两个优点。首先,标记接口定义了一个由标记类的实例实现的类型;标记注释没有。这种类型的存在允许您在编译时捕获错误,如果您使用标记注释,则在运行时之前无法捕获这些错误....
Personally I think I'll bow to Joshua's superior knowledge on this subject.
我个人认为我会屈服于约书亚在这个主题上的卓越知识。
回答by Chanikag
The main purpose is to tell the compiler that treat differently for the object of the class which implemented marker interface.
主要目的是告诉编译器对实现标记接口的类的对象进行不同的处理。
回答by user3293519
Looking carefully on marker interface in Java e.g. Serializable, Clonnable and Remote it looks they are used to indicate something to compiler or JVM. So if JVM sees a Class is Serializable it done some special operation on it, similar way if JVM sees one Class is implement Clonnable it performs some operation to support cloning. Same is true for RMI and Remote interface. So in short Marker interface indicate, signal or a command to Compiler or JVM.
仔细查看 Java 中的标记接口,例如 Serializable、Clonnaable 和 Remote,看起来它们用于向编译器或 JVM 指示某些内容。所以如果 JVM 看到一个类是可序列化的,它就会对它做一些特殊的操作,类似的方式,如果 JVM 看到一个类是实现可克隆的,它就会执行一些操作来支持克隆。RMI 和远程接口也是如此。因此,简而言之,Marker 接口向编译器或 JVM 指示、信号或命令。
Read more: http://javarevisited.blogspot.com/2012/01/what-is-marker-interfaces-in-java-and.html#ixzz2v6fIh1rw
阅读更多:http: //javarevisited.blogspot.com/2012/01/what-is-marker-interfaces-in-java-and.html#ixzz2v6fIh1rw