java定義接口的關(guān)鍵字之interface

java定義接口的關(guān)鍵字之interface

長(zhǎng)沙一度軟件培訓(xùn)      2022-04-07 06:07:01     9

java定義接口的關(guān)鍵字之interface,java定義接口的關(guān)鍵字interface,interface這個(gè)關(guān)鍵字產(chǎn)生一個(gè)完全抽象的類,它根本就沒(méi)有提供任何具體的實(shí)現(xiàn),它允許創(chuàng)建者確定方

課程價(jià)格 請(qǐng)咨詢

上課時(shí)段: 授課校區(qū):

詳細(xì)介紹

java定義接口的關(guān)鍵字interface,interface這個(gè)關(guān)鍵字產(chǎn)生一個(gè)完全抽象的類,它根本就沒(méi)有提供任何具體的實(shí)現(xiàn),它允許創(chuàng)建者確定方法名.參數(shù)列表和返回類型,但沒(méi)有任何方法體,接口只提供了形式,而未提供任何具體實(shí)現(xiàn)

一個(gè)接口表示:"所有實(shí)現(xiàn)了該特定接口的類看起來(lái)都像這樣".接口被用來(lái)建立類與類之間的協(xié)議(某些面向?qū)ο笳Z(yǔ)言用關(guān)鍵字protocol來(lái)完成這一功能.)

要想創(chuàng)建一個(gè)接口,需要用interface關(guān)鍵字代替class關(guān)鍵字,就像類一樣,可以在interface前添加public關(guān)鍵字(但僅限于該接口在與其同名的文件中被定義),如果不加public,則只有包訪問(wèn)權(quán)限,這樣就只能在同一個(gè)包內(nèi)可用,接口也可以包含域,但是這些域都隱式地是static和final的

要讓一個(gè)類遵循某個(gè)特定接口(或者是一組接口),需要使用implements關(guān)鍵字,它表示"interface只是它的外貌,但是現(xiàn)在我要聲明它如何工作的."除此之外,它看起來(lái)很像繼承

恰當(dāng)?shù)脑瓌t是優(yōu)先選擇類而不是接口,從類開(kāi)始,如果接口的必要性變的非常明確,那么就進(jìn)行重構(gòu).

//:?interfaces/music5/Music5.java//?Interfaces.package?object;import?static?net.mindview.util.Print.*;enum?Note{????MIDDLE_C,MIDDLE_D,MIDDLE_F}interface?Instrument?{??//?Compile-time?constant:??int?VALUE?=?5;?//?static?&?final?可以聲明域,但這些域都隱式地是static和final的??//?Cannot?have?method?definitions:??void?play(Note?n);?//?Automatically?public?//自動(dòng)的是public??void?adjust();}class?Wind?implements?Instrument?{??public?void?play(Note?n)?{????print(this?+?".play()?"?+?n);??}??public?String?toString()?{?return?"Wind";?}??public?void?adjust()?{?print(this?+?".adjust()");?}}class?Percussion?implements?Instrument?{??public?void?play(Note?n)?{????print(this?+?".play()?"?+?n);??}??public?String?toString()?{?return?"Percussion";?}??public?void?adjust()?{?print(this?+?".adjust()");?}}class?Stringed?implements?Instrument?{??public?void?play(Note?n)?{????print(this?+?".play()?"?+?n);??}??public?String?toString()?{?return?"Stringed";?}??public?void?adjust()?{?print(this?+?".adjust()");?}}class?Brass?extends?Wind?{??public?String?toString()?{?return?"Brass";?}}????class?Woodwind?extends?Wind?{??public?String?toString()?{?return?"Woodwind";?}}public?class?Music5?{??//?Doesn't?care?about?type,?so?new?types??//?added?to?the?system?still?work?right:??static?void?tune(Instrument?i)?{????//?...????i.play(Note.MIDDLE_C);??}??static?void?tuneAll(Instrument[]?e)?{????for(Instrument?i?:?e)??????tune(i);??}??????public?static?void?main(String[]?args)?{????//?Upcasting?during?addition?to?the?array:????Instrument[]?orchestra?=?{??????new?Wind(),??????new?Percussion(),??????new?Stringed(),??????new?Brass(),??????new?Woodwind()????};????tuneAll(orchestra);??}}?//:~

繼承和接口可以在同一個(gè)類同時(shí)使用

//: polymorphism/Sandwich.java// Order of constructor calls.package ch08;interface FastFood{? ? void show();}class Meal {? Meal() { System.out.println("Meal()"); }}class Bread {? Bread() { System.out.println("Bread()"); }}class Cheese {? Cheese() { System.out.println("Cheese()"); }}class Lettuce {? Lettuce() { System.out.println("Lettuce()"); }}class Lunch extends Meal {? Lunch() { System.out.println("Lunch()"); }}class PortableLunch extends Lunch {? PortableLunch() { System.out.println("PortableLunch()");}}public class Sandwich extends PortableLunch implements FastFood{ //繼承和接口可以在同一個(gè)類同時(shí)使用? private Bread b = new Bread();? private Cheese c = new Cheese();? private Lettuce l = new Lettuce();? public void show()? {? ? ? System.out.println("pushing your sandwich order");? }? public Sandwich()? {? ? ? System.out.println("Sandwich()");?? ? ? show();? }? public static void main(String[] args) {? ? new Sandwich();? }} //:~

以上就是長(zhǎng)沙一度軟件培訓(xùn)java培訓(xùn)機(jī)構(gòu)的小編針對(duì)“java定義接口的關(guān)鍵字之interface”的內(nèi)容進(jìn)行的回答,希望對(duì)大家有所幫助,如有疑問(wèn),請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。

培訓(xùn)啦提醒您:交易時(shí)請(qǐng)核實(shí)對(duì)方資質(zhì),對(duì)于過(guò)大宣傳或承諾需謹(jǐn)慎!任何要求預(yù)付定金、匯款等方式均存在風(fēng)險(xiǎn),謹(jǐn)防上當(dāng)。