• Java中的标记接口(标签接口)


    Java中的标记接口(Marker Interface),又称标签接口(Tag Interface),具体是不包含任何方法的接口。

    简单一句话描述

    标记接口就是用来判断某个类是否具有某个能力的

    首先明确一点,Marker Interface(标记接口)决不是Java这门编程语言特有的,而是计算机科学中一种通用的设计理念

    The tag/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.

    上面这段话是维基百科对标记接口的定义。 

    具体说的就是:标记接口是计算机科学中的一种设计思路。编程语言本身不支持为类维护元数据。而标记接口则弥补了这个功能上的缺失——一个类实现某个没有任何方法的标记接口,实际上标记接口从某种意义上说就成为了这个类的元数据之一。运行时,通过编程语言的反射机制,我们就可以在代码里拿到这种元数据。 

    具体分析

    Serializable接口

    java.io.Serializable这个接口是用来标记类是否支持序列化的,所谓的序列化就是将对象的各种信息转换成可以存储或者传输的一种形式。如果一个类没有实现该接口,却被拿去序列化,那么虚拟机就会抛出不支持序列化的异常。

    下面的代码JDK源代码中摘出来的:

    1. if (obj instanceof String) {
    2. writeString((String) obj, unshared);
    3. } else if (cl.isArray()) {
    4. writeArray(obj, desc, unshared);
    5. } else if (obj instanceof Enum) {
    6. writeEnum((Enum) obj, desc, unshared);
    7. } else if (obj instanceof Serializable) {
    8. writeOrdinaryObject(obj, desc, unshared);
    9. } else {
    10. if (extendedDebugInfo) {
    11. throw new NotSerializableException(cl.getName() + " "
    12. + debugInfoStack.toString());
    13. } else {
    14. throw new NotSerializableException(cl.getName());
    15. }
    16. }

    Java里进行序列化,字符串,数组,枚举类和普通类是分别进行的。如果当前待序列化的变量既不是字符串,也不是数组和枚举类,那么就检测该类是否实现了Serializable的接口,如果没有实现Serializable接口,就会抛出异常NotSerializableException。 

    Cloneable接口

    在深度拷贝的时候经常被用到

    Cloneable注释也注明了如果一个类没有实现Cloneable接口,在执行clone方法时会抛出CloneNotSupportedException异常。

    1. /**
    2. * A class implements the Cloneable interface to
    3. * indicate to the {@link java.lang.Object#clone()} method that it
    4. * is legal for that method to make a
    5. * field-for-field copy of instances of that class.
    6. *

    7. * Invoking Object's clone method on an instance that does not implement the
    8. * Cloneable interface results in the exception
    9. * CloneNotSupportedException being thrown.
    10. *

    11. * By convention, classes that implement this interface should override
    12. * Object.clone (which is protected) with a public method.
    13. * See {@link java.lang.Object#clone()} for details on overriding this
    14. * method.
    15. *

    16. * Note that this interface does not contain the clone method.
    17. * Therefore, it is not possible to clone an object merely by virtue of the
    18. * fact that it implements this interface. Even if the clone method is invoked
    19. * reflectively, there is no guarantee that it will succeed.
    20. *
    21. * @author unascribed
    22. * @see java.lang.CloneNotSupportedException
    23. * @see java.lang.Object#clone()
    24. * @since JDK1.0
    25. */
    26. public interface Cloneable {
    27. }

    我们都知道java.lang.Object类中本身已经有了一个方法:

    protected native Object clone() throws CloneNotSupportedException

    但是被protected修饰,作用域是同一个包下,也就是说如果想使用的话需要在子类调用super.clone(),那我们使用Cloneable接口就能知道该类具有克隆的功能。

    RandomAccess

    这个接口的作用是判断集合是否能快速访问,也就是通过索引下标能否快速的移动到对应的元素上。我们在使用某个集合类中,集合中的元素可以通过索引index下标快速的访问到,那么在该类的定义处,一般会有一个RandomAccess接口的实现标签

    比如:java.util.ArrayList有这个接口,java.util.LinkedList就没有

    1. public class ArrayList extends AbstractList
    2. implements List, RandomAccess, Cloneable, java.io.Serializable
    1. public class LinkedList
    2. extends AbstractSequentialList
    3. implements List, Deque, Cloneable, java.io.Serializable

    大家也许会问,在Spring里满天飞的注解(Annotation)不是最好的用来维护元数据的方式么?确实,Annotation能声明在Java包、类、字段、方法、局部变量、方法参数等的前面用于维护元数据的目的,既灵活又方便。然而这么好的东西,只有在JDK1.5之后才能用。JDK1.5之前维护元数据的重任就落在标记接口上了。 

    参考文章:

    什么是Java Marker Interface(标记接口) - 腾讯云开发者社区-腾讯云 (tencent.com)

    java中的标记接口_内功心法 -- Java标记接口_矿泉水指挥官的博客-CSDN博客

  • 相关阅读:
    3.Redis的5种数据结构
    MQTT介绍和使用
    Mock.js使用
    Unity复刻骑砍中的帝国象棋(一)
    认识wireshark
    ChatGLM2-6B 部署
    【车联网应用笔记】OTA从理论到落地
    计算机专业毕业设计题目大全——各种类型系统设计大全
    linux系统Jenkins工具介绍
    优化开发人员对 K8s 安全的影响
  • 原文地址:https://blog.csdn.net/qq_51409098/article/details/126848132