📜  Java中的标记接口

📅  最后修改于: 2022-05-13 01:54:23.916000             🧑  作者: Mango

Java中的标记接口

它是一个空接口(没有字段或方法)。标记接口的示例是可序列化、可克隆和远程接口。这些接口都是空接口。

public interface Serializable 
{
  // nothing here
}

在实时应用程序中使用的标记接口示例:

  1. 可克隆接口:可克隆接口存在于Java.lang 包中。 Object 类中有一个方法 clone()。实现 Cloneable 接口的类表明 clone() 方法对该类的实例进行逐个字段的复制是合法的。
    在未实现 Cloneable 接口的类的实例上调用 Object 的 clone 方法会导致抛出异常 CloneNotSupportedException。按照惯例,实现此接口的类应该重写 Object.clone() 方法。
    有关详细信息,请参阅此处。
    Java
    // Java program to illustrate Cloneable interface
    import java.lang.Cloneable;
      
    // By implementing Cloneable interface
    // we make sure that instances of class A
    // can be cloned.
    class A implements Cloneable
    {
        int i;
        String s;
      
        // A class constructor
        public A(int i,String s)
        {
            this.i = i;
            this.s = s;
        }
      
        // Overriding clone() method
        // by simply calling Object class
        // clone() method.
        @Override
        protected Object clone()
        throws CloneNotSupportedException
        {
            return super.clone();
        }
    }
      
    public class Test
    {
        public static void main(String[] args)
            throws CloneNotSupportedException
        {
            A a = new A(20, "GeeksForGeeks");
      
            // cloning 'a' and holding
            // new cloned object reference in b
      
            // down-casting as clone() return type is Object
            A b = (A)a.clone();
      
            System.out.println(b.i);
            System.out.println(b.s);
        }
    }


    Java
    // Java program to illustrate Serializable interface
    import java.io.*;
      
    // By implementing Serializable interface
    // we make sure that state of instances of class A
    // can be saved in a file.
    class A implements Serializable
    {
        int i;
        String s;
      
        // A class constructor
        public A(int i,String s)
        {
            this.i = i;
            this.s = s;
        }
    }
      
    public class Test
    {
        public static void main(String[] args)
          throws IOException, ClassNotFoundException
        {
            A a = new A(20,"GeeksForGeeks");
      
            // Serializing 'a'
            FileOutputStream fos = new FileOutputStream("xyz.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(a);
      
            // De-serializing 'a'
            FileInputStream fis = new FileInputStream("xyz.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            A b = (A)ois.readObject();//down-casting object
      
            System.out.println(b.i+" "+b.s);
      
            // closing streams
            oos.close();
            ois.close();
        }
    }



    输出:
    20
    GeeksForGeeks
  2. 序列化接口:可序列化接口存在于Java.io 包中。它用于使对象有资格将其状态保存到文件中。这称为序列化。
    未实现此接口的类将不会对其任何状态进行序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。

    Java

    // Java program to illustrate Serializable interface
    import java.io.*;
      
    // By implementing Serializable interface
    // we make sure that state of instances of class A
    // can be saved in a file.
    class A implements Serializable
    {
        int i;
        String s;
      
        // A class constructor
        public A(int i,String s)
        {
            this.i = i;
            this.s = s;
        }
    }
      
    public class Test
    {
        public static void main(String[] args)
          throws IOException, ClassNotFoundException
        {
            A a = new A(20,"GeeksForGeeks");
      
            // Serializing 'a'
            FileOutputStream fos = new FileOutputStream("xyz.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(a);
      
            // De-serializing 'a'
            FileInputStream fis = new FileInputStream("xyz.txt");
            ObjectInputStream ois = new ObjectInputStream(fis);
            A b = (A)ois.readObject();//down-casting object
      
            System.out.println(b.i+" "+b.s);
      
            // closing streams
            oos.close();
            ois.close();
        }
    }
    


    输出:
    20 GeeksForGeeks
  3. 远程接口:远程接口存在于Java.rmi 包中。远程对象是存储在一台机器上并从另一台机器访问的对象。因此,要使对象成为远程对象,我们需要使用 Remote 接口对其进行标记。在这里,远程接口用于标识可以从非本地虚拟机调用其方法的接口。任何作为远程对象的对象都必须直接或间接地实现该接口。 RMI(远程方法调用)提供了一些远程对象实现可以扩展的便利类,以方便远程对象的创建。