关于Java中的标记接口的一些知识

再次问好! 这次,我想谈谈Java中的标记接口。 每个Java开发人员都看到过它们,尽管不是每个人都知道这种类型的接口有一个特定的词。 在这篇文章中,我想重点介绍以下几点:什么是标记接口,我们拥有哪些内置标记接口以及如何将它们与另一个概念(注释)进行比较。

What is a marker interface?

标记接口基本上是空的,它们没有方法或字段(例如常量)。 我们使用它们来提供有关对象的运行时信息。 举个例子,我来一个内置的标记界面java.lang.Cloneable。 尽管有一种方法,但它不会克隆类本身Object.clone()每个Java类都会继承。 但是通过使用Cloneable接口,我们表明一个类实现了一个克隆逻辑,调用该类是合法的克隆()用于对该类的实例进行逐域复制的方法。 否则,致电克隆()未实现Cloneable的类的方法将导致抛出CloneNotSupportedException。 首先让我们看一下Java中的内置标记接口。

Built-in marker interfaces in Java

Java为我们提供了3个内置标记接口:

  • java.lang.Cloneablejava.io.Serializablejava.rmi.Remote

您可以遵循Javadoc的这些接口,并确保它们完全适合标记器接口的上述定义。 因此,我们已经在上一节中讨论了Cloneable接口。 让我们快速浏览一下其余的接口。

java.io.Serializable用于使对象可序列化。 序列化将Java对象写为字节序列,并从字节流中读取这些对象。 通过以下方式实现ObjectOutputStream用于编写(或序列化)并通过ObjectInputStream用于阅读(或反序列化)。 所以为了上课可序列化,我们需要实现此标记接口。 否则我们可以收到NotSerializableException例如在打电话时ObjectOutputStream.writeObject(). By the way, all child classes (subclasses) of a 可序列化 class are also 可序列化 (we would talk about this in a momemt).

Ĵava.rmi.Remote is a bit more complicated and rare, but it still makes sense to talk about it briefly here. This interface is used to identify interfaces whose methods may be invoked from a non-local virtual machine. Any object that is a remote object must directly or indirectly implement this interface. Only those methods specified in a remote interface (this is an interface that extends java.rmi.Remote) are available remotely. You can read more about remote interfaces in this good Oracle's tutorial on remote interface design.

在Java中还有另一个可能看起来相似的概念-注解. In next section let overview what is a difference between 注解 and marker interfaces.

Marker interfaces vs. annotations

Java annotations may seem similar to marker interfaces as they allow us to do same thing. We can apply an annotation to any class to indicate that it can be used in specific logic. And there is sometimes a strong voice to abandon marker interfaces at all and switch to annotations. But are they 100% similar? Consider a following code snippet:

class Document implements Serializable{

    //..some fields
}

因此,我们使我们的文档可序列化,并且可以像之前所说的那样通过序列化来写入/读取它。 但是,请考虑是否要制作一些具体文档:

class Spreadsheet extends Document{}

class Diagram extends Document{}

//...

如果我们不想使图可序列化怎么办? 但是我们不能采用这种配置。 标记接口遵循多态逻辑,因此可序列化类的所有子代也可序列化。 另一个逻辑是,如果我们使用样本@可序列化注解:

class Document{}

@Serializable
class Spreadsheet extends Document{}

class Diagram extends Document{}

顺便说说文献也可以是标记界面,但我们将在以后的文章中讨论。

Conclusion

In this short post we discussed marker interfaces: what is it, what are built-in marker interfaces and what is a difference between them and annotations. All Java developers in some degree encountred marker interfaces, but not everyone pay attention to that in reality it is a separate pattern. Hope this short article made you more interested in this concept. You can also read the original version of this post about marker interfaces in Java in my blog. Have a nice day!

References

  • Gaurav Miglani. Marker interfaces in Java. GeeksForGeeks. Read here
  • Mainak Goswami. Is there a better approach to Marker? (2012). DZone. Read here

from: https://dev.to//andreevich/a-bit-about-marker-interfaces-in-java-b1e

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值