简体   繁体   English

Java中标记接口的用途是什么?

[英]What is the use of marker interfaces in Java?

当标记接口中没有任何东西可以实现时,比如Serializable实现它有什么用?

Joshua Bloch: Effective Java 2nd Edition, p 179 Joshua Bloch: Effective Java 第二版,第 179 页

Item 37: Use marker interfaces to define types第 37 条:使用标记接口定义类型

... You may hear it said that marker annotations (Item 35) make marker interfaces obsolete. ... 您可能听说过标记注释(第 35 项)使标记接口过时。 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....这种类型的存在允许您在编译时捕获错误,如果您使用标记注释,则在运行时之前无法捕获这些错误....

Personally I think I'll bow to Joshua's superior knowledge on this subject.我个人认为我会屈服于约书亚在这个主题上的卓越知识。

In earlier versions of Java, Marker Interfaces were the only way to declare metadata about a class.在 Java 的早期版本中,标记接口是声明类元数据的唯一方法。 For example, the Serializable Marker Interface lets the author of a class say that their class will behave correctly when serialized and deserialized.例如,Serializable Marker Interface 让类的作者说他们的类在序列化和反序列化时会正确运行。

In modern Java, marker interfaces have no place.在现代 Java 中,标记接口没有位置。 They can be completely replaced by Annotations , which allow for a very flexible metadata capability.它们可以完全被Annotations取代,这允许非常灵活的元数据功能。 If you have information about a class, and that information never changes, then annotations are a very useful way to represent it.如果你有关于一个类的信息,而且这些信息永远不会改变,那么注释是一种非常有用的表示它的方式。

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.现在首选注释,因为它们不会传播到子类。

See Marker interface pattern .请参阅标记接口模式

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 decision and 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.您不想使所有内容都可序列化 - 否则您可能会通过序列化意外暴露(例如)密码或其他敏感数据。

They are called marker interfaces.它们被称为标记接口。 And as the name implies, they mark that 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.已经讨论过它们是否不应该被注解取代,因为它们的功能非常相似。

If you implement an interface then instanceof will be true.如果您实现了一个接口,则instanceof将为 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.如果您的接口没有要实现的内容,那么您可以使用它来标记具有元数据的类,就像 Java 1.5 及更高版本的注释一样,而不必强制实现者做任何特殊的事情。

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 注释实现——(再次)它们的大部分效果将通过基于反射的代码实现。

Looking carefully on marker interface in Java eg Serializable, Clonnable and Remote it looks they are used to indicate something to compiler or JVM.仔细查看 Java 中的标记接口,例如 Serializable、Clonnaable 和 Remote,看起来它们用于向编译器或 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.因此,如果 JVM 看到一个类是可序列化的,它就会对它进行一些特殊的操作,类似的方式,如果 JVM 看到一个类是实现可克隆的,它就会执行一些操作来支持克隆。 Same is true for RMI and Remote interface. RMI 和远程接口也是如此。 So in short Marker interface indicate, signal or a command to Compiler or JVM.因此,简而言之,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

主要目的是告诉编译器对实现标记接口的类的对象进行不同的处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM