資源描述:
《Java講義5.ppt》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、入門第五講類,繼承作業(yè)4Card.javaConstructorprintOutPlayer.javaConstructorplayFirst(),play(),winTurn()Belote.javamain()Card類publicclassCard{intvalue;Stringname;intsuit;}Card構(gòu)造函數(shù)publicclassCard{publicintvalue;publicStringname;publicintsuit;publicCard(intv,Strings,intn){value=v;name=s;suit=n;}}Cardprintout(
2、)publicclassCard{publicintvalue;publicStringname;publicintsuit;publicCard(intv,Strings,intn){value=v;name=s;suit=n;}publicprintOut(){System.out.println(value+““+name+““+suit);}Player類publicclassPlayer{publicStringname;publicintscore;publicintcounter;publicCard[]cards;}Player構(gòu)造函數(shù)publicclassPlay
3、er{publicStringname;publicintscore;publicintcounter;publicCard[]cards;publicPlayer(Strings){name=s;cards=newCard[13];score=0;counter=0;}}PlayeraddCard()publicclassPlayer{publicStringname;publicintscore;publicintcounter;publicCard[]cards;publicCardaddCard(Cardc){cards[counter]=c;counter++;}}Bel
4、ote類publicclassBelote{staticCardcards[];staticPlayerplayers[];}Belote構(gòu)造函數(shù)publicclassBelote{staticCardcards[];staticPlayerplayers[];publicBelote(){//createplayersplayers=newPlayer[4];players[0]=newPlayer(“Adam”);players[1]=newPlayer(“Bob”);players[2]=newPlayer(“Charlie”);players[3]=newPlayer(“D
5、aniel”);}}卡片排列publicclassBelote{staticCardcards[];staticPlayerplayers[];publicBelote(){intcount=0;//distributecardsfor(inti=0;i<4;i++){for(intj=1;j<=13;j++){players[i].addCard(cards[count]);count++;}}}}Belotemain()publicclassBelote{staticCardcards[];staticPlayerplayers[];staticpublicvoidmain(S
6、tring[]args){//createanewinstanceofBeloteBelotegame=newBelote();}}publicclassBelote{staticCardcards[];staticPlayerplayers[];staticpublicvoidmain(String[]args){//createanewinstanceofBeloteBelotegame=newBelote();//loopthroughplayersfor(inti=0;i<4;i++){for(intj=0;j<13;j++){players[i].cards[j].pri
7、ntOut();}}空對象空對象意味著什么都沒有。刪掉數(shù)組中的一個(gè)元素很容易,但是也意味著沒有辦法挽回。publicclassPlayer{publicStringname;publicintscore;publicintcounter;publicCard[]cards;publicCardplayFirst(){if(counter==0){returnnull;}else{counter--;returncards[0];//錯(cuò)誤!!}}}cards[0]may