java4android (介面的基本文法)

來源:互聯網
上載者:User

標籤:android   style   blog   class   c   code   

什麼是介面?定義了介面,就是定義了調用對象的標準

介面的基本文法:

1.使用interface定義;

2.介面當中的方法都是抽象方法;

3.介面當中的方法都是public許可權


介面的基本文法(二):

1.實現介面使用implements關鍵字

2.一個類可以實現多個介面

3.一個介面可以繼承多個介面

舉例:

interface USB{//定義了介面    public void read();    public void write();}
interface Wifi{//定義了Wifi介面    public void open();        public void close();}
class Phone implements USB,Wifi{//implements 實現 USBPhone實現USB介面    //實現介面中的全部抽象方法    public void read(){        System.out.print("read");    }        public void write(){        System.out.print("write");    }        public void open(){        System.out.print("Wifi Open");    }        public void close(){        System.out.print("Wifi close");    }}
class Test{    public static void main(String args[]){        Phone phone = new Phone();        USB usb = phone;//向上轉型        usb.read();        usb.write();                Wifi wifi = phone;//向上轉型        wifi.open();        wifi.close();     }}

一個介面可以繼承多個介面舉例:

interface A{    Public void funA();}
interface B{    Public void funB();}
interface C extends A,B{//C不是實現A,B介面,而是繼承A,B。所以C裡裡面有A、B、C介面的所有方法,//當有一個類來實現C介面的時候,就要複寫A、B、C所有的方法    Public void funC();}

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.