北京北大青鳥校區(qū)解讀:Java接口的原理和作用

北京北大青鳥校區(qū)學(xué)術(shù)部老師提供:

什么是JAVA接口?
北大青鳥校區(qū)專家講解:接口的概念其實(shí)并不難理解,接口關(guān)鍵字Interface,在使用時(shí)可以只定義函數(shù)體而不需要具體的實(shí)現(xiàn)。再類的繼承過程中可以實(shí)現(xiàn)多個(gè)接口而取代了類的多繼承。使用接口其實(shí)就有點(diǎn)像實(shí)現(xiàn)虛函數(shù)的調(diào)用一樣,用繼承接口的子類實(shí)例化聲名得借口就可以通過接口調(diào)用子類內(nèi)部接口定義的函數(shù)。使用這種接口方式編程,如果業(yè)務(wù)邏輯發(fā)生變化需要新增類多方法,就可以再不改變原來已經(jīng)寫好的代碼基礎(chǔ)上新增一個(gè)類來實(shí)現(xiàn)接口中定義的函數(shù)來實(shí)現(xiàn)。具體方法請看下面兩個(gè)例子:(北京北大青鳥校區(qū)

1、JAVA多態(tài)接口動(dòng)態(tài)加載實(shí)例

用來計(jì)算每一種交通工具運(yùn)行1000公里所需的時(shí)間,已知每種交通工具的參數(shù)都是3個(gè)整數(shù)A、B、C的表達(dá)式。現(xiàn)有兩種工具:(北京北大青鳥校區(qū)
Car 和Plane,其中Car 的速度運(yùn)算公式為:A*B/C
Plane 的速度運(yùn)算公式為:A+B+C。

需要編寫三類:ComputeTime.java,Plane.java,Car007.java和接口Common.java,要求在未來如果增加第3種交通工具的時(shí)候,不必修改以前的任何程序,只需要編寫新的交通工具的程序。其運(yùn)行過程如下,從命令行輸入ComputeTime的四個(gè)參數(shù),第一個(gè)是交通工具的類型,第二、三、四個(gè)參數(shù)分別時(shí)整數(shù)A、B、C,舉例如下:(北京北大青鳥校區(qū)
計(jì)算Plane的時(shí)間:"java ComputeTime Plane 20 30 40"
計(jì)算Car007的時(shí)間:"java ComputeTime Car007 23 34 45"
如果第3種交通工具為Ship,則只需要編寫Ship.java,運(yùn)行時(shí)輸入:"java ComputeTime Ship 22 33 44"
提示:充分利用接口的概念,接口對象充當(dāng)參數(shù)。
實(shí)例化一個(gè)對象的另外一種辦法:Class.forName(str).newInstance();例如需要實(shí)例化一個(gè)Plane對象的話,則只要調(diào)用Class.forName("Plane").newInstance()便可。(北京北大青鳥校區(qū)
Java代碼:
1. import CalTime.vehicle.all.Common; 
2. import java.lang.*; 
3. public interface Common ...{ 
4. double runTimer(double a, double b, double c); 
5. } 
6. public class Plane implements Common ...{ 
7. public double runTimer(double a, double b, double c) ...{ 
8. return (a+ b + c); 
9. } 
10. } 
11. public class Car implements Common ...{ 
12. public double runTimer(double a, double b, double c) ...{ 
13. return ( a*b/c ); 
14. } 
15. } 
16. public class ComputeTime ...{ 
17. public static void main(String args[]) ...{ 
18. System.out.println("交通工具: "+args[0]); 
19. System.out.println(" 參數(shù)A: "+args[1]); 
20. System.out.println(" 參數(shù)B: "+args[2]); 
21. System.out.println(" 參數(shù)C: "+args[3]); 
22. double A=Double.parseDouble(args[1]); 
23. double B=Double.parseDouble(args[2]); 
24. double C=Double.parseDouble(args[3]); 
25. double v,t; 
26. try ...{ 
27. Common d=(Common) Class.forName("CalTime.vehicle."+args[0]).newInstance(); 
28. v=d.runTimer(A,B,C); 
29. t=1000/v; 
30. System.out.println("平均速度: "+v+" km/h"); 
31. System.out.println("運(yùn)行時(shí)間:"+t+" 小時(shí)"); 
32. } catch(Exception e) ...{ 
33. System.out.println("class not found"); 
34. } 
35. } 
36. }
以前看過一個(gè)求形狀的題目就是有兩個(gè)圓形求交集現(xiàn)在定義了兩種情況問要是擴(kuò)展大別的情況應(yīng)當(dāng)怎么設(shè)計(jì),想了很久不得其解,現(xiàn)在忽然覺得接口通殺矣~(北京北大青鳥校區(qū)

2、JAVA接口作為參數(shù)傳遞

可以將借口類型的參數(shù)作為方法參數(shù),在實(shí)際是使用時(shí)可以將實(shí)現(xiàn)了接口的類傳遞給方法,后方法或按照重寫的原則執(zhí)行,實(shí)際調(diào)用的是實(shí)現(xiàn)類中的方法代碼體,這樣便根據(jù)傳進(jìn)屋的參數(shù)的不同而實(shí)現(xiàn)不同的功能。重要的是,當(dāng)我以后徐要林外一個(gè)對象并且擁有接受說生命的方法的時(shí)候的時(shí)候,我們不必須原類,只需新的類實(shí)現(xiàn)借口即可。(北京北大青鳥校區(qū)
Java代碼:
1. import java.lang.*; 
2. interface Extendbroadable ...{ 
3. public void inPut(); 
4. } 
5. class KeyBroad implements Extendbroadable ...{ 
6. public void inPut() ...{ 
7. System.out.println(" hi,keybroad has be input into then mainbroad! "); 
8. } 
9. } 
10. class NetCardBroad implements Extendbroadable ...{ 
11. public void inPut() ...{ 
12. System.out.println(" hi,netCardBroad has be input into then mainbroad! "); 
13. } 
14. } 
15. class CheckBroad ...{ 
16. public void getMainMessage(Extendbroadable ext)...{ 
17. ext.inPut(); 
18. } 
19. } 
20. public class InterfaceTest01 ...{ 
21. public static void main(String []args) ...{ 
22. KeyBroad kb=new KeyBroad(); 
23. NetCardBroad ncb=new NetCardBroad(); 
24. CheckBroad cb=new CheckBroad(); 
25. cb.getMainMessage(kb); 
26. cb.getMainMessage(ncb); 
27. } 
28. }
希望本文的介紹,能給你帶來幫助。(北京北大青鳥校區(qū)


 

北大青鳥網(wǎng)上報(bào)名
北大青鳥招生簡章