欢迎来到天天文库
浏览记录
ID:49581232
大小:506.50 KB
页数:44页
时间:2020-02-25
《Java讲义5.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、入门第五讲类,继承作业4Card.javaConstructorprintOutPlayer.javaConstructorplayFirst(),play(),winTurn()Belote.javamain()Card类publicclassCard{intvalue;Stringname;intsuit;}Card构造函数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构造函数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构造函数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();}}空对象空对象意味着什么都没有。删掉数组中的一个元素很容易,但是也意味着没有办法挽回。publicclassPlayer{publicStringname;publicintscore;publicintcounter;publicCard[]cards;publicCardplayFirst(){if(counter==0){returnnull;}else{counter--;returncards[0];//错误!!}}}cards[0]may
此文档下载收益归作者所有