简体   繁体   English

Marker接口和空接口之间的区别?

[英]Difference between Marker interface and empty interface?

Every Marker interface is an empty interface. 每个Marker接口都是一个空接口。 Is vice-versa also correct? 反之亦然吗?

Every Marker interface is an empty interface. 每个Marker接口都是一个空接口。 Is vice-versa also correct? 反之亦然吗?

Nope. 不。 An empty interface that has no meaning is not a marker interface ... in any useful sense. 在任何有用的意义上,没有意义的空接口都不是标记接口。 A marker interface has to denote something meaningful about the class that implements it. 标记器接口必须表示一些有关实现它的类的有意义东西

And, in fact, according to some definitions a (hybrid) marker interface can contain methods (see the Wikipedia definition below). 而且,实际上,根据某些定义,(混合)标记接口可以包含方法(请参见下面的Wikipedia定义)。 So a marker interface isn't necessarily an empty interface. 因此,标记器接口不一定是空接口。


@Sudheep Vallipoyil says in his answer. @Sudheep Vallipoyil在回答中说。

1) It is not possible to create a user-defined marker interface. 1)无法创建用户定义的标记界面。

I disagree. 我不同意。 That is based on a narrow definition of "marker interface" that is not the commonly accepted definition. 这是基于“标记接口”的狭窄定义,而不是普遍接受的定义。

By contrast, here's how Wikipedia defines "marker interface" and the corresponding design pattern. 相比之下,这是Wikipedia如何定义“标记界面”和相应的设计模式。

"The marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. It provides a means to associate metadata with a class where the language does not have explicit support for such metadata." “标记接口模式是计算机科学中的一种设计模式,与提供有关对象的运行时类型信息的语言一起使用。它提供了一种方法,可以将元数据与该语言不明确支持此类的类相关联。”

" To use this pattern, a class implements a marker interface (also called tagging interface), and methods that interact with instances of that class test for the existence of the interface. Whereas a typical interface specifies functionality (in the form of method declarations) that an implementing class must support, a marker interface need not do so. The mere presence of such an interface indicates specific behavior on the part of the implementing class. Hybrid interfaces, which both act as markers and specify required methods, are possible but may prove confusing if improperly used." 要使用这种模式,一个类实现一个标记器接口(也称为标记接口),并且与该类的实例进行交互的方法将测试该接口的存在。而典型的接口则指定了功能(以方法声明的形式)实现类必须支持的,而标记接口则不必这样做。仅存在这样的接口就表示实现类方面的特定行为,混合接口既可以充当标记,又可以指定所需的方法,但是可能如果使用不当,会造成混乱。”

A second definition from Joshua Bloch says: 约书亚·布洛赫(Joshua Bloch)的第二个定义是:

"A marker interface is an interface that contains no method declaration, but merely designates (or "marks") a class that implements the interface as having some property." “标记器接口是一种不包含方法声明的接口,而只是指定(或“标记”)一个实现该接口具有某些属性的类。

As you can see, in normal accepted usage , the term "marker interface" is not reserved for interfaces defined by the JVM. 如您所见,在正常接受的用法中 ,术语“标记接口”不是为JVM定义的接口保留的。 Anyone can define one. 任何人都可以定义一个。

See also: 也可以看看:

At first shot we can call an interface as marker interface if it does not contain any members and it is used to give an indication to JVM about the run time behavior of the implementing class. 乍一看,如果接口不包含任何成员,并且可以用来向JVM指示实现类的运行时行为,则可以将其称为标记接口。 Every empty interface is not aa marker interface as 每个空接口都不是标记接口,因为

1)It is not possible to create a user-defined marker interface 1)无法创建用户定义的标记界面

2)The empty interface you are writing still can extend any other interfaces which are not empty (then even the empty interface has some inherited members). 2)您正在编写的空接口仍然可以扩展任何其他不为空的接口(然后,即使该空接口具有某些继承的成员)。

It is better to understand like the need to use a marker interface is replaced with the use of annotations from java 5.(by this you can understand what an marker interface meant for) 最好理解一下,就像使用标记接口代替了Java 5中的注释一样(由此您可以了解标记接口的含义)。

This answer is to comment on @Stephen C on his answer 这个答案是对@Stephen C的评论

1.The question is not what is Marker interface pattern.(It is like asking what is iterator and what is iterator pattern) 1,问题不是什么是Marker接口模式(就像问什么是迭代器和什么是迭代器模式)

2.The wiki definition is for the 'marker interface design pattern' not for marker interface concept in java 2. Wiki定义适用于“标记界面设计模式”,而不适用于Java中的标记界面概念

3.from effective java by Joshua you can find the following definition for marker interface 3从Joshua的有效Java中可以找到标记接口的以下定义

"A marker interface is an interface that contains no method declaration,but merely designates(or "marks") a class that implements the interface as having some property." “标记器接口是不包含方法声明的接口,而仅指定(或“标记”)一个实现该接口具有某些属性的类。”

He also explained why Set(collection frame work) which intact an empty interface is not a Marker interface. 他还解释了为什么完整保留一个空接口的Set(集合框架起作用)不是Marker接口。

i do't think the answer should be something like copying some definitions from wiki and explain the same(that also the unrelated one !) 我不认为答案应该类似于从Wiki复制一些定义并解释相同的内容(那也是无关的一个内容!)

Your wrong!!! 你的失误!!! I do not know where you are getting this information at? 我不知道您从哪里得到这些信息? From java SE 7 Programmers Study Guide mentions that “interface is a public set of methods that must be implemented by the class that uses the interface.” An interface is an interface, if it has changed I should read this in this type of book mention above. 从Java SE 7程序员学习指南中提到,“接口是一组公共方法,必须由使用该接口的类来实现。”接口是接口,如果已更改,则应在此类书中阅读以上。 If an interface like Cloneable has no public methods, means it does not have public methods, but any class that implements it must implement the Object.clone() as public. 如果像Cloneable这样的接口没有公共方法,则意味着它没有公共方法,但是实现该接口的任何类都必须将Object.clone()实现为公共。 This is what it means!!! 这是什么意思!!! Please, don't make something so simple into a complex discussion, especially at an interview!! 请不要将简单的事情变成复杂的讨论,尤其是在采访中!!

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

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