在jdk的源码中,存在这样的一些接口,他们不包含任何的(抽象)方法,可是却普遍的存在。java
这种接口咱们称之为Mark Interface,也就是标记接口。数组
这些接口呢,咱们不用来实现任何的方法,他们的做用就是当某个类实现这个接口的时候,咱们就认为这个类拥有了这个接口标记的某种功能了。dom
下面经过三个例子,分别介绍java中经常使用的三个标记接口:ide
RandomAccess 、Cloneable、java.io.Serializableui
(1)RandomAccess this
在C#中常常会有不少人在争论,在遍历集合时,究竟是应该用for仍是用foreach。spa
在Java中,却彻底不用再纠结这个问题:指针
java中有这样一个接口code
1 public interface RandomAccess { 2 }
这个接口的做用是判断集合是否能快速访问的。也就是传入一个Index后,指针可否快速的移动到对应的元素上,仍是须要像访问队列同样依次移动到指定元素上。server
若是咱们在实现某个容器类时,容器(防盗链接:本文首发自http://www.cnblogs.com/jilodream/ )中的元素能够经过index快速的访问到(通常核心的存储接口是使用数组),那么你在该类的定义处,就能够像ArrayList这样打上一个RandomAccess接口的实现标签:
1 public class ArrayList<E> extends AbstractList<E>
2 implements List<E>, RandomAccess, Cloneable, java.io.Serializable 3 { 4 //...
5 } 6 而LinkedList就没有实现该标记接口 7 public class LinkedList<E>
8 extends AbstractSequentialList<E>
9 implements List<E>, Deque<E>, Cloneable, java.io.Serializable 10 { 11 //...
12 }
在使用的过程当中,经过判断是否实现RandomAccess接口,就能够决定采起哪一种遍历的方式了。
以下:
1 import java.util.List; 2 import java.util.RandomAccess; 3
4 public class SourceLearning 5 { 6 public void iteratorElements(List<String> list) 7 { 8 if (list instanceof RandomAccess) 9 { 10 for (int i = 0, size = list.size(); i < size; i++) 11 { 12 String element = list.get(i); 13 } 14 } 15 else
16 { 17 for (String element : list) 18 { 19 } 20 } 21 } 22 }
这样针对于不一样的List采起不一样的遍历形式,可让遍历的速度更快。
(2)Cloneable
这个接口你们都很是熟悉,在深度拷贝的时候,经常用到该接口。这个接口也是一个标记接口:
1 public interface Cloneable { 2 }
他的做用是标记该对象的是否拥有克隆的能力。不少认或许会以为疑惑,Object类自己就已经实现了 protected native Object clone() throws CloneNotSupportedException;
方法
按道理来讲每个类都应该能够运行clone方法的,为何还须要打上这样一个接口。这样的好处是以接口的形式标记对象是否拥有某种能力。想想,假若不经过标记接口的形式,咱们在日常的工做中,如何实现呢?通常来讲都是经过设定枚举,或者增长变量来控制。这样或许能解决问题,可是每每不能从面向对象的角度来优雅的解决问题。
想一想接口的做用是什么吧?接口就是用来标记某个类拥有了哪些功能、特性。而标记接口则是在面向对象的角度来看,更高层级的一种抽象:即便你拥有这个方法也不行,由于你没有这个功能的标记接口。
因此在调用的clone的过程当中,假若对象没有实现Cloneable 接口,那么虚拟就会抛出一个CloneNotSupportedException,不支持的clone的异常。
(3)java.io.Serializable
这个接口是用来标记类是否支持序列化的。所谓的序列化就是将对象的各类信息转化成能够存储或者传输的一种形式。我记得我刚参加工做的时候,对这个序列化很是难以理解,以为server返回一个对象,client接收便可,为何总要(防盗链接:本文首发自http://www.cnblogs.com/jilodream/ )序列化,反序列化的折腾。后来leader告诉我这是由于不少时候,因为通讯协议的缘由,在传输的过程当中,复杂的类对象是不支持传来传去的,因此通常来讲要转化成流的形式,放在包中传来传去。言归正传,java.io.Serializable和Cloneable 接口同样,假若一个类没有实现该接口,而被拿去序列化,虚拟机就会抛出不支持的异常,尽管从代码的调用来讲,不存在任何问题。
请看java源码注释中的第二个抛出异常:一个将要被序列化,可是未实现序列化接口的Object:
1 /**
2 * Write the specified object to the ObjectOutputStream. The class of the 3 * object, the signature of the class, and the values of the non-transient 4 * and non-static fields of the class and all of its supertypes are 5 * written. Default serialization for a class can be overridden using the 6 * writeObject and the readObject methods. Objects referenced by this 7 * object are written transitively so that a complete equivalent graph of 8 * objects can be reconstructed by an ObjectInputStream. 9 * 10 * <p>Exceptions are thrown for problems with the OutputStream and for 11 * classes that should not be serialized. All exceptions are fatal to the 12 * OutputStream, which is left in an indeterminate state, and it is up to 13 * the caller to ignore or recover the stream state. 14 * 15 * @throws InvalidClassException Something is wrong with a class used by 16 * serialization. 17 * @throws NotSerializableException Some object to be serialized does not 18 * implement the java.io.Serializable interface. 19 * @throws IOException Any exception thrown by the underlying 20 * OutputStream. 21 */
22 public final void writeObject(Object obj) throws IOException { 23 if (enableOverride) { 24 writeObjectOverride(obj); 25 return; 26 } 27 try { 28 writeObject0(obj, false); 29 } catch (IOException ex) { 30 if (depth == 0) { 31 writeFatalException(ex); 32 } 33 throw ex; 34 } 35 }
至此,经过三种经常使用的标记接口,应该已经阐述清标记接口的使用状况了,我的认为这是一种很是抽象的面向对象的方式。即只经过向对象以添加标签的形式,来标记这个对象能够或不能够实现某种功能,这要比直接定义变量或枚举,要优雅的多。