JAVA_介面(Interface)_上


介面一詞到底要如何去瞭解

事實上於日常生活中我們有很多具體東西已經知道可以用Class的觀念去看待
但是有些事務實際實作上各自都不同
是比較不能很具體、明確地去實作出一套流程、如何具體實作等等!!!!
也因此介面很抽象更不能實體化
預設的存取修飾為public 、 abstract

此時就會透過"介面" 來去定義

介面和抽象類別有點像
只定義方法但不實作
差別在於抽象類別中也可能包含實作過的方法成員
還有介面中不可定義任一屬性,除非是一種常數(不可更改)。
需要用final去修飾


介面是被類別實作
介面可被介面繼承、可去多重繼承(繼承多個父類別)
且凡是定義於介面中所有方法成員都是抽象的 
因此也不用特定用abstract做修飾描述
有去實作(implements) interface的物件必須要去實作其中所定義的方法
介面事實上就是一套規格標準的定義
JAVA中 interface中的方法存取權限都會是public公開的
主要就是要利於其他類去實作
所以可以省略abstract 和 public 修飾


介面定義


interface InterfaceName {
 [public][static][final] varProperty;
 [public][abstract] method:
}



介面實作語法如下

class ClassName implements InterfaceName , InterfaceName2,....
{
   ...
}

可將其裡解為
使用 介面 InterfaceName 來打造 類別 ClassName





介面  可以解釋為一種身分資格(抽象概念)
介面實作則可想成  想獲取就必須去付出一些證照考取的準備過程
那每個人都有各自不同的準備方法


舉例而言

我想成為某一領域的專家(JAVA 專家 或是 Linux 專家)
就需要去考過某張認證
有人是去透過做考古題、有人是透過做中學
每個人去實作的方式皆各有不同
有人只想獲取一個資格
有人則想同時獲取兩個資格

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaceex;

interface JavaExpert{
    void OCJP();
}

interface LinuxExpert{
    void LPIC101();
    void LPIC102();
}

class DiligentEngineer implements JavaExpert , LinuxExpert{

    @Override
    public void OCJP() {
        System.out.println("K OCJP的書,做題庫");
    }

    @Override
    public void LPIC101() {
        System.out.println("K LPIC101的書,做題庫");
    }

    @Override
    public void LPIC102() {
        System.out.println("K LPIC102的書,做題庫");
    }
    
}

class JavaEngineer implements JavaExpert{

    @Override
    public void OCJP() {
        System.out.println("從工作中學習,或是透過做作品累積學習。");
    }
    
}



/**
 *
 * @author chous
 */
public class InterfaceEx {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        DiligentEngineer eng1 = new DiligentEngineer();
        System.out.println("第一位勤勉的工程師想考取OCJP、LPIC101、LPIC102");
        eng1.OCJP();
        eng1.LPIC101();
        eng1.LPIC102();
        
        JavaEngineer eng2 = new JavaEngineer();
        System.out.println("第二位JAVA工程師只想考OCJP,取得Java專家認證資格,提升競爭力");
        eng2.OCJP();
    }    
}



介面  也可去實踐 「多型」 的觀念
在此他又有點像為了達成某個目的而定制出來的共同標準

今日你的系統需要去開啟一份檔案
可能是一部影音也可能是一張圖片我將此動作定義於檔案介面之中
此時媒體影音的類  與  圖片的類 都去透過檔案介面
來去實作所定義的開啟檔案這個抽象方法



各自去實作各的回傳結果等等


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaceex2;

interface File {
    String getFileName();
}

class MultiMedia implements File{
    @Override
    public String getFileName() {
        return "Media File Name";
    }
}

class Picture implements File{

    @Override
    public String getFileName() {
        return "Image File Name";
    }
    
}

/**
 *
 * @author chous
 */
public class InterfaceEx2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        File fileVideo = new MultiMedia();
        System.out.println("開啟影音 :" + fileVideo.getFileName());
        
        File fileImage = new Picture();
        System.out.println("開啟圖片 :" + fileImage.getFileName());
    }
    
}


一般在工作上接觸到比較大或比較有規模的系統時
會發現遇上層其架構會愈抽象
此種設計規劃
主要也是為了後續專案中子區塊的明確任務切割
假設從剛剛的例子中要去多增設一個網路連線傳送
的功能

那我們一樣可以再多增開一個介面去實踐


我們要透過網路介面進行建立連結
傳送接收到的目標檔案路徑
之後再斷線



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaceex2;

interface File {

    String getFileName();
}

interface Network {

    void connect();

    void disconnect();

    void transfer(String fileName);
}

class MultiMedia implements File {

    @Override
    public String getFileName() {
        return "Media File Name";
    }
}

class Picture implements File {

    @Override
    public String getFileName() {
        return "Image File Name";
    }
}

/**
 *
 * @author chous
 */
public class InterfaceEx2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        File fileVideo = new MultiMedia();
        System.out.println("開啟影音 :" + fileVideo.getFileName());

        File fileImage = new Picture();
        System.out.println("開啟圖片 :" + fileImage.getFileName());
    }

    public void process(File file, Network net) {
        net.connect();
        net.transfer(file.getFileName());
        net.disconnect();
    }
}

所以我們在設計上只會需考量到流程方面
其他任務區塊
比方說檔案部分交給其他人去設計實踐
網路也是看其他人要透過TCP或UDP的不同協定去實踐

這就是其能夠達到明確切割分工的好處所在!!!!!



Can an interface be private/protected?

https://coderanch.com/t/254867/certification/interface-private-protected


In Java, can an interface be protected, private or final?

https://www.quora.com/In-Java-can-an-interface-be-protected-private-or-final




留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題