版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
實(shí)驗(yàn)一實(shí)驗(yàn)題目體操比賽計(jì)算選手成績(jī)的辦法是去掉一個(gè)最高分和一個(gè)最低分再計(jì)算平均分,而學(xué)??疾煲粋€(gè)班級(jí)的某科目的考試情況時(shí),是計(jì)算全班學(xué)生的平均成績(jī)。Gymnastics類和School類都實(shí)現(xiàn)了ComputerAverage接口,但實(shí)現(xiàn)方式不同。程序代碼interfaceComputerAverage{?publicdoubleaverage(doublex[]);}classGymnasticsimplementsComputerAverage{ publicdoubleaverage(doublex[]){? intcount=x.length; doubleaver=0,temp=0; for(inti=0;i<count;i++){ ? for(intj=i;j<count;j++){? ??if(x[j]<x[i]){ temp=x[i]; ? ??x[i]=x[j]; ??x[j]=temp; ?? } ? }? }??for(inti=1;i<count-1;i++){ ??aver=aver+x[i];? } if(count>2)??aver=aver/(count-2); else? aver=0; returnaver; }}classSchoolimplementsComputerAverage{ publicdoubleaverage(doublex[]){? intcount=x.length;??doubleaver=0;? for(inti=0;i<count;i++){? aver=aver+x[i]; } ?if(count>0) ?aver=aver/count; returnaver;}}publicclassEstimat(yī)or{ publicstaticvoidmain(Stringargs[]){??doublea[]={9.89,9.88,9.99,9.12,9.69,9.76,8.97}; ?doubleb[]={89,56,78,90,100,77,56,45,36,79,98}; ?ComputerAveragecomputer; computer=newGymnastics(); doubleresult=computer.average(a);//computer調(diào)用average(doublex[])方法,將數(shù)組a傳遞給參數(shù)x ?System.out.printf("%n");??System.out.printf("體操選手最后得分:%5.3f\n",result);? computer=newSchool();??result=computer.average(b);//computer調(diào)用average(doublex[])方法,將數(shù)組b傳遞給參數(shù)x ?System.out.printf("班級(jí)考試平均分?jǐn)?shù):%-5.2f\n",result);?}}實(shí)驗(yàn)結(jié)果實(shí)驗(yàn)分析一個(gè)類可以實(shí)現(xiàn)多個(gè)接口,類通過使用關(guān)鍵字implements聲明自己實(shí)現(xiàn)一個(gè)或多個(gè)接口,假如一個(gè)非抽象類實(shí)現(xiàn)了某個(gè)接口,那么這個(gè)類必須重寫該接口的所有方法。實(shí)驗(yàn)練習(xí)School類假如不重寫publicdoubleaversge(doublex[])方法,程序編譯時(shí)提醒如何的錯(cuò)誤?答:SChool不是抽象的,并且未覆蓋ComputerAverage中的抽象方法。實(shí)驗(yàn)二實(shí)驗(yàn)題目貨車要裝載一批貨品,貨品由三種商品組成:電視、計(jì)算機(jī)和洗衣機(jī),卡車需要計(jì)算出整批貨品的重量。實(shí)驗(yàn)代碼interfaceComputerWeight{ publicdoublecomputerWeight();}classTelevisionimplementsComputerWeight{?publicdoublecomputerWeight(){? return3.5;?}}classComputerimplementsComputerWeight{?publicdoublecomputerWeight(){ return2.67;?}}classWashMachineimplementsComputerWeight{ publicdoublecomputerWeight(){ return13.8; }}classTruck{ ComputerWeight[]goods; doubletotalWeights=0;?Truck(ComputerWeight[]goods){ ?this.goods=goods;??}?publicvoidsetGoods(ComputerWeight[]goods){? this.goods=goods;?}?publicdoublegetTotalWeights(){ ?totalWeights=0; for(inti=0;i<goods.length;i++){? ?totalWeights=totalWeights+goods[i].computerWeight(); ?}??returntotalWeights;?}}publicclassCheckCarWeight{ publicstaticvoidmain(Stringargs[]){? ComputerWeight[]goods=newComputerWeight[650];//裝載650件貨品 for(inti=0;i<goods.length;i++){//提成三類 ? if(i%3==0) ??goods[i]=newTelevision(); ??elseif(i%3==1)? ?goods[i]=newComputer();? ?elseif(i%3==2)?? goods[i]=newWashMachine(); ?} Trucktruck=newTruck(goods); System.out.printf("\n貨車裝載的貨品重量:%-8.5fkg\n",truck.getTotalWeights()); goods=newComputerWeight[68];//68件貨品??for(inti=0;i<goods.length;i++){//提成兩類? if(i%2==0) ? goods[i]=newTelevision(); ?else ? goods[i]=newWashMachine();??}? truck.setGoods(goods);? System.out.printf("貨車裝載的貨品重量:%-8.5fkg\n",truck.getTotalWeights()); }}實(shí)驗(yàn)結(jié)果實(shí)驗(yàn)分析接口回調(diào)是指:可以把使用某一接口的類型創(chuàng)建的對(duì)象引用賦給該接口聲明的接口變量中,那么該接口變量就可以調(diào)用被實(shí)現(xiàn)的接口中的方法,當(dāng)接口變量調(diào)用被類實(shí)現(xiàn)的接口中的方法時(shí),就是告知相應(yīng)的對(duì)象調(diào)用接口的方法,這一過程成為對(duì)象功能的接口回調(diào)。接口的方法不一定相同,接口回調(diào)也許產(chǎn)生不同的行為。實(shí)驗(yàn)練習(xí)請(qǐng)?jiān)趯?shí)驗(yàn)基礎(chǔ)上再編寫一個(gè)實(shí)現(xiàn)ComputerWeight接口的類,比如Refrigerrator。這樣一來,貨車裝載的貨品中就可以有Refrigerrator類型的對(duì)象。當(dāng)系統(tǒng)增長(zhǎng)一個(gè)實(shí)現(xiàn)ComputerWeight接口的類后,Truck類需要進(jìn)行修改嗎?答:代碼:classRefrigerratorimplementsComputerWeight{?publicdoublecomputerWeight(){ ?return12.8; }}實(shí)驗(yàn)三實(shí)驗(yàn)題目小狗在不同環(huán)境條件下也許呈現(xiàn)不同的狀態(tài)表現(xiàn),規(guī)定接口封裝小狗的狀態(tài)。具體規(guī)定如下:(1)編寫一個(gè)接口DogStat(yī)e,該接口有一個(gè)名為voidshowState()方法。(2)編寫一個(gè)Dog類,該類中有一個(gè)DogState接口聲明的變量state,此外,該類有一個(gè)show()方法,在該方法中讓接口stat(yī)e回調(diào)showStat(yī)e()方法。(3)編寫若干個(gè)實(shí)現(xiàn)DogState接口的類,負(fù)責(zé)刻畫小狗的各種狀態(tài)。(4)編寫主類,在主類中實(shí)現(xiàn)測(cè)試小狗的各種狀態(tài)。程序代碼interfaceDogState{?publicvoidshowState();}classSoftlyStat(yī)eimplementsDogState{?publicvoidshowState(){??System.out.println("聽主人的命令");?}}classMeetEnemyStat(yī)eimplementsDogState{ publicvoidshowState(){? System.out.println("狂叫,并沖過去狠咬敵人"); }}classMee(cuò)tFriendStateimplementsDogState{ publicvoidshowState(){ ?System.out.println("晃動(dòng)尾巴,表達(dá)歡迎"); }}classMeetAnotherdogStateimplementsDogState{ publicvoidshowState(){? System.out.println("嬉戲"); }}classDog{ DogStatestate;?publicvoidshow(){ state.showState(); } publicvoidsetStat(yī)e(DogStates){ ?state=s;?}}publicclassCheckDogState{ publicstaticvoidmain(Stringargs[]){ DogyellowDog=newDog();??System.out.print("狗在主人面前:");??yellowDog.setStat(yī)e(newSoftlyState());? yellowDog.show();? System.out.print("狗碰到敵人:"); ?yellowDog.setStat(yī)e(newMeetEnemyState()); yellowDog.show();? System.out.print("狗碰到朋友:");??yellowDog.setState(newMeetFriendState());??yellowDog.show();??System.out.print("狗碰到同類:"); yellowDog.setState(newMeetAnotherdogState());? yellowDog.show();?}}實(shí)驗(yàn)結(jié)果實(shí)驗(yàn)分析面向接口編程是指當(dāng)設(shè)計(jì)某種重要的類時(shí),不讓該類面向具體的類,而是面向接口,即所設(shè)計(jì)中的重要數(shù)據(jù)是接口聲明的變量,而不是具體聲明的對(duì)象。5、實(shí)驗(yàn)練習(xí)用面向接口的思想編寫一個(gè)程序,模擬水杯中的水在不同溫度下也許出現(xiàn)的狀態(tài)。代碼:interfaceWaterState{?publicvoidshowState();}classSubzeroStateimplementsWaterStat(yī)e{ publicvoidshowState(){ ?System.out.println("結(jié)冰");?}}classNormalStateimplementsWaterState{?publicvoidshowState(){? System.out.println("冰冷或涼快"); }}classHotStateimplementsWat(yī)erState{?publicvoidshowState(){ System.out.println("有熱氣冒出,溫?zé)?);?}}classBoiledStat(yī)eimplementsWaterState{?publicvoidshowState(){ System.out.println("沸騰,燙");?}}classWater{ Wat(yī)erStatestate;?publicvoidshow(){? state.showStat(yī)e();?} publicvoidsetStat(yī)e(WaterStates){? state=s; }}publicclassCheckWat(yī)erState{ publicstaticvoidmain(Stringargs[]){ WatercupWat(yī)er=newWater(); ?System.out.print("水杯中的水在零下時(shí):"); cupWater.setState(newSubzeroState());??cup
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度南京企業(yè)總部辦公室高端裝修合同4篇
- 2025版門衛(wèi)突發(fā)事件應(yīng)對(duì)合同范本4篇
- 2025年度智能穿戴設(shè)備研發(fā)與制造承攬服務(wù)合同范本4篇
- 擔(dān)保合同協(xié)議書(2篇)
- 二零二五版智能門禁系統(tǒng)研發(fā)與定制合同全文4篇
- 二零二五年度出租車行業(yè)司機(jī)招聘與綠色出行倡導(dǎo)合同3篇
- 二零二五年度門類安裝工程質(zhì)量保證合同4篇
- 2025年棉花產(chǎn)業(yè)扶貧項(xiàng)目運(yùn)輸保障合同書2篇
- 二零二五年度排水設(shè)施安全保障與應(yīng)急預(yù)案合同4篇
- 二零二五年度土地租賃合同糾紛調(diào)解服務(wù)協(xié)議
- 家具生產(chǎn)車間規(guī)章制度
- (高清版)JTGT 3360-01-2018 公路橋梁抗風(fēng)設(shè)計(jì)規(guī)范
- 小紅書違禁詞清單(2024年)
- 胰島素注射的護(hù)理
- 云南省普通高中學(xué)生綜合素質(zhì)評(píng)價(jià)-基本素質(zhì)評(píng)價(jià)表
- 2024年消防產(chǎn)品項(xiàng)目營(yíng)銷策劃方案
- 聞道課件播放器
- 03軸流式壓氣機(jī)b特性
- 五星級(jí)酒店收入測(cè)算f
- 大數(shù)據(jù)與人工智能ppt
- 人教版八年級(jí)下冊(cè)第一單元英語(yǔ)Unit1 單元設(shè)計(jì)
評(píng)論
0/150
提交評(píng)論