Java 中的標記介面或帶標籤介面是什麼?
本文將幫助您瞭解 Java 中的標記介面或帶標籤介面是什麼。在瞭解標記介面之前,讓我們回顧一下介面。
介面
與物件類似,介面是類的藍圖。它包含靜態常量和抽象方法。它是 Java 中實現抽象和多重繼承的一種機制。它使用 interface 關鍵字宣告。它提供完全抽象,這意味著介面中的所有方法都必須宣告為空體,並且所有欄位預設情況下都必須是 public、static 和 final。
語法
interface <interface_name>{ // constant fields declaration // abstract methods declaration // by default }
標記介面
沒有方法、欄位和常量的介面稱為標記介面或帶標籤介面。這種型別的介面是一個空介面。它提供有關物件執行時資訊。此資訊傳遞給 JVM 和編譯器。可序列化和可克隆介面是標記介面的兩個示例。
語法
interface Serializable { }
標記介面的用途
標記介面透過訊息通知 Java 編譯器,以便它向實現它的類新增一些特殊特性。如果有關類的資訊保持不變,則可以使用標記介面來表示該資訊。它主要用於 API 開發和 Spring 等框架中。
標記介面的型別
JDK 中存在許多內建的標記介面或帶標籤介面。下面顯示了 3 個最常用的介面:
可序列化介面
可克隆介面
遠端介面
讓我們詳細瞭解一下每個介面。
可序列化介面
可序列化介面是一個標記介面,它在 java.io 包中定義。要使類可序列化,必須實現 Serializable 介面。然後,根據需要可以序列化或反序列化該類的物件。
Java 中的序列化是一種將物件的狀態轉換為位元組流的方式。它的主要用途是在網路上傳輸物件的狀態。反序列化是序列化的逆過程,其中從序列化狀態重建物件。必須具有物件定義才能成功地重新建立它。
語法
ObjectInputStream in = new ObjectInputStream(inputStream); Student s = (MyObject)in.readObject();
示例
Student.java import java.io.Serializable; // importing java.io package for serializable interface public class Student implements Serializable { // class declaration for implementing serializable interface int studentId; // variable declaration String studentName; // variable declaration public Student(int studentId, String studentName){ this.studentId = studentId; this.studentName = studentName; } } StudentSerialization.java import java.io.*;// java.io package imported class StudentSerialization{ // class declaration public static void main(String[] args) { // main function declaration try { // try block Student std = new Student(64,"JohnWick"); // creating object using Student class FileOutputStream fout=new FileOutputStream("Student File.txt"); // creating stream and writing the object ObjectOutputStream out=new ObjectOutputStream(fout); out.writeObject(std); out.flush(); out.close(); // closing the stream System.out.println("Data read from the file"); } catch(Exception e) { // catch block e.printStackTrace(); } } }
輸出
Data read from the file
可克隆介面
可克隆介面是一個標記介面,它在 java.lang 包中定義。它使用不同的名稱克隆物件。此介面支援 Object 類的 clone() 方法。如果在類中未實現此介面,而不管是否呼叫 clone() 方法,它都會丟擲 ClassNotSupportedException。
示例
import java.util.Scanner; // java.util package imported public class Clone implements Cloneable { // class declaration along with cloneable interface int studentId; String studentName; int studentClass; public Clone (int studentId, String studentName, int studentClass) { //Clone class constructor this.studentId = studentId; this.studentName = studentName; this.studentClass = studentClass; } public void printList() { // function to print details System.out.println("Student ID: "+studentId); System.out.println("Student Name: "+studentName); System.out.println("Student Class: "+studentClass); } public static void main (String args[]) throws CloneNotSupportedException { // main function declaration Scanner sc = new Scanner(System.in); // Scanner class object created System.out.print("Enter Student ID: "); // Reading user given values int studentId = sc.nextInt(); System.out.print("Enter Student name: "); String studentName = sc.next(); System.out.print("Enter Student Class: "); int studentClass = sc.nextInt(); System.out.println("-------Student Detail--------"); Clone p1 = new Clone(studentId, studentName, studentClass); Clone p2 = (Clone) p1.clone(); //cloning the object of the Student class using the clone() method p2.printList(); // invoking the method to print detail } }
輸出
Enter Student ID: 64 Enter Student name: JohnWick Enter Student Class: 1 -------Student Detail-------- Student ID: 64 Student Name: JohnWick Student Class: 1
遠端介面
遠端介面是一個標記介面,它在 java.rmi 包中定義。它將物件標記為遠端物件,該物件可由另一臺機器(主機)訪問。實現此介面後,物件可用作遠端物件。它還識別其函式可以從 JVM 呼叫 的介面。任何遠端物件都必須直接或間接地實現該介面。
語法
import java.rmi.*; public interface Multiply extends Remote{ public int mul(int p, int r, int t)throws RemoteException' }
自定義介面
自定義介面可以由使用者建立,即它不是像上面三個介面那樣的內建介面。
語法
interface <interface_name> { }
示例
interface Bullet720 { // custom marker interface } class Train implements Bullet720 { // class that implements the Bullet720 marker interface static void isTrain() {// static function to display System.out.println("Bullet720 is a Train."); } } public class CustomInterface { // main class declaration public static void main(String args[]) { // main function declaration Train.isTrain(); // invoking methods of the class Train } }
輸出
Bullet720 is a Train.