Learn Java for Android Development Second Edition 筆記(六)- Interface

來源:互聯網
上載者:User

標籤:java   android   

InterfaceJava裡用interface關鍵字,引入一種沒有具體實現的類型。Declaring Interfacesinterface一般以大寫字母開頭,able單詞結束,如下例子:
interface Drawable{int RED = 1; // For simplicity, integer constants are used. These constants areint GREEN = 2; // not that descriptive, as you will see.int BLUE = 3;int BLACK = 4;void draw(int color);}
interface聲明的幾點注意事項:
  • 如果需要被別的package訪問,interface前可以加public,否則預設只能被package內部的其他類訪問。
  • interface前沒有必要再加abstract,因為它實際上已經是abstract的了。
  • interface裡面的成員變數的屬性是public final static,沒有必要再顯示加上這三個屬性。
  • interface裡面的成員變數必須顯示初始化。
  • interface裡面的成員函數屬性是public abstract,沒有必要再顯示加上這兩個屬性。
  • interface裡面的成員函數不能聲明為static的,因為interface裡的成員函數都是instance method。
  • 沒有成員的interface稱為marker interface
Drawable介面聲明了要做什麼,但是沒有定義怎麼做。有實現了該介面的類去定義怎麼做。Implementing Interfaces
class Point implements Drawable{@Overridepublic void draw(int color){System.out.println("Point drawn at " + toString() + " in color " + color);}}
上面實現介面要注意兩個地方,一個是draw函數前要加上public,因為介面定義裡上public,另外一個是加上@Override。可以把一個對象的引用賦值給該對象裡實現了的介面類型的變數,例如以下例子:
public static void main(String[] args){Drawable[] drawables = new Drawable[] { new Point(10, 20), new Circle(10, 20, 30) };for (int i = 0; i < drawables.length; i++)drawables[i].draw(Drawable.RED);}
以上方法也實現了多態。一個對象如果實現了兩種介面,那麼對象轉換成的這兩種介面之見可以互相轉換,如下例子:
public static void main(String[] args){Drawable[] drawables = new Drawable[] { new Point(10, 20),new Circle(10, 20, 30) };for (int i = 0; i < drawables.length; i++)drawables[i].draw(Drawable.RED);Fillable[] fillables = new Fillable[drawables.length];for (int i = 0; i < drawables.length; i++){fillables[i] = (Fillable) drawables[i];fillables[i].fill(Fillable.GREEN);}}
一個類如果同時實現多個介面,有可能會有命名衝突,例如兩個介面裡有相同名字和參數的函數。Extending Interfacesinterface可以繼承,例如以下例子:
interface Colors{int RED = 1;int GREEN = 2;int BLUE = 3;int BLACK = 4;}interface Drawable extends Colors{void draw(int color);}interface Fillable extends Colors{void fill(int color);}
Why Use Interfaces?Java裡面提供interface介面並不僅是因為沒有提供多重繼承的功能,而是 更為重要地實現一個理念:將介面和實現分離,面向介面編程(介面可以由interface或abstract class實現)。只要有可能,盡量在代碼裡指定介面而不是具體類,以便代碼能自適應的變更,特別是使用Java的Collections架構時。以下是一個不靈活的例子:
ArrayList<String> arrayList = new ArrayList<String>();void dump(ArrayList<String> arrayList){// suitable code to dump out the arrayList}
上面例子中將ArrayList類具體到了很多地方,如果某天想要變換使用一種類型的List如LinkedList,那麼就需要修改很多地方。以上例子可以改成依賴於介面而不是具體的實作類別,例如:
List<String> list = new ArrayList<String>();void dump(List<String> list){// suitable code to dump out the list}
改成以上方式後,如果要換用LinkedList,那麼只需要修改一個地方就可以了。Interface和Abstract Class的幾點總結:
  • 這兩個是Java提供的兩種抽象類別型,不能被直接執行個體化
  • Interface不依賴於任何類,可以有任何類來實現。
  • Abstract Class可以和Interface聯合起來使用。


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.