Java - interface

By sunwc 2023-03-22 Java

interface 特性

  1. interface 可以定義全局常量、抽象方法、靜態方法與默認方法

  2. interface 沒有 constructor,因此也不可以實例化

  3. Java類可以 implements 多個 interface ,彌補了 Java 單繼承的侷限性 (先寫繼承後寫實現)

public final class String extends Object implements java.io.Serializable, Comparable<String>, CharSequence {

}
  1. interface 之間可以多重繼承
  2. interface 體現多型的特性
  3. 可以把 interface 看作是一種規範 (contract)

現實例子: USB adaptor、JDBC interface 資料庫連線的規範(MySQL Driver, MSSQL Driver, Oracle Driver,….)

例子

/**
 * interface USB
 * @Description
 * @author sunwc
 * @Date 2023年3月22日下午12:31:17
 *
 */
interface USB {
    // 全局常量:定義了長、寬、高、最小傳輸速度等
    public static final int WIDTH = 3;

    // 開始驅動
    public abstract void start(); // public abstract可省略
    // 移除驅動
    void stop();

    // 靜態方法(有方法體)
    public static void staticMethod() {
         System.out.println("interface USB靜態方法");
    }

    // 默認方法(有方法體)
    public default void defaultMethod1() {
         System.out.println("interface USB默認方法1");
    }

    public default void defaultMethod2() {
         System.out.println("interface USB默認方法2");
    }

}


	/**
	 * 創見USB 實現 interface USB
	 * @Description
	 * @author sunwc
	 * @Date 2023年3月22日下午12:33:16
	 *
	 */
	class Transcend implements USB {

	    @Override
	    public void start() {
	        System.out.println("Transcend USB開啟工作");
	    }
	    @Override
	    public void stop() {
	        System.out.println("Transcend USB結束工作");
	    }
	    @Override
	    public void defaultMethod1() {
	        System.out.println("Transcend覆寫defaultMethod1");
	    }
	    
	    public void myMethod() {
            // 調用自己定義的覆寫方法
            defaultMethod1();
            // 調用interface中的默認方法
            USB.super.defaultMethod1();
        }
	}

	/**
	 * 電腦類
	 * @Description
	 * @author sunwc
	 * @Date 2023年3月22日下午12:33:01
	 *
	 */
	class Computer {
	    
        /**
		 * 傳輸資料
		 * @Description
		 * @author sunwc
		 * @Date 2023年3月22日下午2:58:08
		 * @param usb
		 */
	    public void transferData(USB usb) {
	        usb.start();

	        System.out.println("具體傳輸資料細節");

	        usb.stop();
	    }
	}

	/**
	 * 測試類
	 * @Description
	 * @author sunwc
	 * @Date 2023年3月22日下午12:33:27
	 *
	 */
	public class USBTest {

	    public static void main(String[] args) {
	        Computer computer = new Computer();

	        Transcend transcend = new Transcend();// Usb usb = new Transcend(); 
	        computer.transferData(transcend);

	        System.out.println("=============================");
	        
	        // interface的靜態方法只能透過interface進行調用
	        USB.staticMethod();
	        // 實現類可以覆寫interface的default方法
	        transcend.defaultMethod1();
	        // 若實現類沒有覆寫interface的default方法,則執行原本default方法的內容
	        transcend.defaultMethod2();
	        
	        System.out.println("==============================");
	        
	        transcend.myMethod();
	    }
	}

輸出結果:

Transcend USB開啟工作
具體傳輸資料細節
Transcend USB結束工作
=============================
interface USB靜態方法
Transcend覆寫defaultMethod1
interface USB默認方法2
==============================
Transcend覆寫defaultMethod1
interface USB默認方法1

結論:

interface 與 抽象類別 都不能被實例化,因此均體現 多型 的特性,需要子類別來繼承 或 實現類來實現

以上面的例子來說,電腦傳輸資料時,均接受符合USB規範的各種物件,這就是多型的體現方式,因為電腦不用管USB adaptor、硬碟 (hard disk) 是哪種牌子,只要是符合interface USB規範都可以,也就能夠透過USB usb變數接受的不同 實現類別 而執行不同的 開始驅動 與 移除驅動 的方法了,藉以展現不同的行為模式