欢迎来到天天文库
浏览记录
ID:41033015
大小:63.50 KB
页数:10页
时间:2019-08-14
《结构化编程与面向对象编程案例》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、*******音乐播放器*******定义一个歌曲清单,程序进行顺序播放,歌曲清单中可以包含不同格式的文件(如MP3,WAV)voidmain(){String[]songs=newString[MAX];//定义一个字符串数组,存放音频文件名songs[0]=newString(“c:彩虹.wav”);songs[1]=newString(“d:天路.mp3”);songs[2]=newString(“c:铃儿响叮当.wav”);……playMusic(songs);}voidplayMusic(String[]list){fot(inti=0;i
2、3、……}使用面向对象方法改造后:abstractclassSong{protectedstringtitle;//音乐名称protectedintduration;//音乐时长Song(stringt){title=t;}abstractvoidplay();}publicclassMp3extendsSong{Mp3(stringt){super(t);duration=……}voidplay(){…….}}publicclassWavextendsSong{Wav(stringt){super(t);duration=……}voidplay(){…….}4、}publicclassPlayList{privateVectorSongs;//节目集合publicPlayList(){Songs=newVector();}publicvoidadd(Songt)//添加节目{Songs.addElement(t);}publicvoidplayAll()//播放全部的节目{Enumerationenum=Songs.elements();while(enum.hasMoreElements())((Song)enum.nextElement()).play();}}publicstaticvoidmain(str5、ing[]args){PlayListlist=newPlayList();list.add(newMp3(“c:彩虹.mp3”));list.add(newMp3(“d:天路.wav”));list.add(newWav(“c:铃儿响叮当.wav”));list.playAll();}********借书程序********结构化的借书程序:voidlendBook(stringbookID,stringreaderID){boolborrowFlag=isBorrowed(readerID,bookID);//查读者是否已借该书intborrow6、edNum=getBorrowedAmount(readerID);//查询读者借书数量if(borrowFlag)messageBox.show("你已经借阅了此书");//提示已借此书elseif(borrowedNum>=5)messageBox.show("您已经借满了5本书");//提示读者借书已达限额else{intamount=getStockAmount(bookID);//查询某本书的在架数量if(amount>0)//有库存{updateStock(bookID,amount-1);//某书的在架数量减1saveLoan(bookID,7、readerID);//保存借书记录}elsemessageBox.show("该书已全部借出");}}面向对象的借书程序:ClassBook{}ClassReader{}ClassLoan{}voidlendBook(stringbookID,stringreaderID){Bookb1=newBook(bookID);Readerr1=newReader(readerID);if(r1.isBorrowed(bookID))//查读者是否已借该书messageBox.show("你已经借阅了此书");//提示已借此书elseif(r1.getBorro8、wedAmount()>=5)//查询读者借书数量messageB
3、……}使用面向对象方法改造后:abstractclassSong{protectedstringtitle;//音乐名称protectedintduration;//音乐时长Song(stringt){title=t;}abstractvoidplay();}publicclassMp3extendsSong{Mp3(stringt){super(t);duration=……}voidplay(){…….}}publicclassWavextendsSong{Wav(stringt){super(t);duration=……}voidplay(){…….}
4、}publicclassPlayList{privateVectorSongs;//节目集合publicPlayList(){Songs=newVector();}publicvoidadd(Songt)//添加节目{Songs.addElement(t);}publicvoidplayAll()//播放全部的节目{Enumerationenum=Songs.elements();while(enum.hasMoreElements())((Song)enum.nextElement()).play();}}publicstaticvoidmain(str
5、ing[]args){PlayListlist=newPlayList();list.add(newMp3(“c:彩虹.mp3”));list.add(newMp3(“d:天路.wav”));list.add(newWav(“c:铃儿响叮当.wav”));list.playAll();}********借书程序********结构化的借书程序:voidlendBook(stringbookID,stringreaderID){boolborrowFlag=isBorrowed(readerID,bookID);//查读者是否已借该书intborrow
6、edNum=getBorrowedAmount(readerID);//查询读者借书数量if(borrowFlag)messageBox.show("你已经借阅了此书");//提示已借此书elseif(borrowedNum>=5)messageBox.show("您已经借满了5本书");//提示读者借书已达限额else{intamount=getStockAmount(bookID);//查询某本书的在架数量if(amount>0)//有库存{updateStock(bookID,amount-1);//某书的在架数量减1saveLoan(bookID,
7、readerID);//保存借书记录}elsemessageBox.show("该书已全部借出");}}面向对象的借书程序:ClassBook{}ClassReader{}ClassLoan{}voidlendBook(stringbookID,stringreaderID){Bookb1=newBook(bookID);Readerr1=newReader(readerID);if(r1.isBorrowed(bookID))//查读者是否已借该书messageBox.show("你已经借阅了此书");//提示已借此书elseif(r1.getBorro
8、wedAmount()>=5)//查询读者借书数量messageB
此文档下载收益归作者所有