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源代码中摘出来的:

if (obj instanceof String) {
	writeString((String) obj, unshared);
} else if (cl.isArray()) {
	writeArray(obj, desc, unshared);
} else if (obj instanceof Enum) {
	writeEnum((Enum) obj, desc, unshared);
} else if (obj instanceof Serializable) {
	writeOrdinaryObject(obj, desc, unshared);
} else {
	if (extendedDebugInfo) {
		throw new NotSerializableException(cl.getName() + " "
		+ debugInfoStack.toString());
	} else {
		throw new NotSerializableException(cl.getName());
	}
}

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

Cloneable接口

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

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

/**
 * A class implements the <code>Cloneable</code> interface to
 * indicate to the {@link java.lang.Object#clone()} method that it
 * is legal for that method to make a
 * field-for-field copy of instances of that class.
 * <p>
 * Invoking Object's clone method on an instance that does not implement the
 * <code>Cloneable</code> interface results in the exception
 * <code>CloneNotSupportedException</code> being thrown.
 * <p>
 * By convention, classes that implement this interface should override
 * <tt>Object.clone</tt> (which is protected) with a public method.
 * See {@link java.lang.Object#clone()} for details on overriding this
 * method.
 * <p>
 * Note that this interface does <i>not</i> contain the <tt>clone</tt> method.
 * Therefore, it is not possible to clone an object merely by virtue of the
 * fact that it implements this interface.  Even if the clone method is invoked
 * reflectively, there is no guarantee that it will succeed.
 *
 * @author  unascribed
 * @see     java.lang.CloneNotSupportedException
 * @see     java.lang.Object#clone()
 * @since   JDK1.0
 */
public interface Cloneable {
}

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

protected native Object clone() throws CloneNotSupportedException

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

RandomAccess

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

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

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

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

参考文章:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值