更新時(shí)間:2020-06-02 16:51:23 來源:動(dòng)力節(jié)點(diǎn) 瀏覽3020次
java定義接口的關(guān)鍵字interface,interface這個(gè)關(guān)鍵字產(chǎn)生一個(gè)完全抽象的類,它根本就沒有提供任何具體的實(shí)現(xiàn),它允許創(chuàng)建者確定方法名.參數(shù)列表和返回類型,但沒有任何方法體,接口只提供了形式,而未提供任何具體實(shí)現(xiàn)
一個(gè)接口表示:"所有實(shí)現(xiàn)了該特定接口的類看起來都像這樣".接口被用來建立類與類之間的協(xié)議(某些面向?qū)ο笳Z言用關(guān)鍵字protocol來完成這一功能.)
要想創(chuàng)建一個(gè)接口,需要用interface關(guān)鍵字代替class關(guān)鍵字,就像類一樣,可以在interface前添加public關(guān)鍵字(但僅限于該接口在與其同名的文件中被定義),如果不加public,則只有包訪問權(quán)限,這樣就只能在同一個(gè)包內(nèi)可用,接口也可以包含域,但是這些域都隱式地是static和final的
要讓一個(gè)類遵循某個(gè)特定接口(或者是一組接口),需要使用implements關(guān)鍵字,它表示"interface只是它的外貌,但是現(xiàn)在我要聲明它如何工作的."除此之外,它看起來很像繼承
恰當(dāng)?shù)脑瓌t是優(yōu)先選擇類而不是接口,從類開始,如果接口的必要性變的非常明確,那么就進(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);
??}
}?/*?Output:
Wind.play()?MIDDLE_C
Percussion.play()?MIDDLE_C
Stringed.play()?MIDDLE_C
Brass.play()?MIDDLE_C
Woodwind.play()?MIDDLE_C
*///:~
繼承和接口可以在同一個(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();
? }
} /* Output:
Meal()
Lunch()
PortableLunch()
Bread()
Cheese()
Lettuce()
Sandwich()pushing your sandwich order
*///:~
以上就是動(dòng)力節(jié)點(diǎn)java培訓(xùn)機(jī)構(gòu)的小編針對“java定義接口的關(guān)鍵字之interface”的內(nèi)容進(jìn)行的回答,希望對大家有所幫助,如有疑問,請?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。
相關(guān)閱讀
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743