• 为什么这些java接口没有抽象方法?浅谈Java标记接口


    在jdk的源码中,存在这样的一些接口,他们不包含任何的(抽象)方法,但是却广泛的存在。

    这种接口我们称之为Mark Interface,也就是标记接口。

    这些接口呢,我们不用来实现任何的方法,他们的作用就是当某个类实现这个接口的时候,我们就认为这个类拥有了这个接口标记的某种功能了。

    下面通过三个例子,分别介绍java中常用的三个标记接口:

    RandomAccess 、Cloneable、java.io.Serializable

    (1)RandomAccess 

    在C#中经常会有很多人在争论,在遍历集合时,到底是应该用for还是用foreach。

    在Java中,却完全不用再纠结这个问题:

    java中有这样一个接口

    1 public interface RandomAccess {
    2 }

    这个接口的作用是判断集合是否能快速访问的。也就是传入一个Index后,指针能否快速的移动到对应的元素上,还是需要像访问队列一样依次移动到指定元素上。

    如果我们在实现某个容器类时,容器(防盗连接:本文首发自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     }

        至此,通过三种常用的标记接口,应该已经阐述清标记接口的使用情况了,个人认为这是一种非常抽象的面向对象的方式。即只通过向对象以添加标签的形式,来标记这个对象可以或不可以实现某种功能,这要比直接定义变量或枚举,要优雅的多。

     

  • 相关阅读:
    删除表
    删除表格的行或者列
    给word中的表格增加行或者列
    向word中插入表格
    设置图片的对齐方式
    day19作业
    Python入门day19——叠加多个装饰器、yield、三元表达式、生成式、函数的递归调用
    day18作业
    Python入门day18——有参装饰器
    Python入门day18——迭代器生成器
  • 原文地址:https://www.cnblogs.com/jilodream/p/5986519.html
Copyright © 2020-2023  润新知