欢迎来到天天文库
浏览记录
ID:39549692
大小:71.50 KB
页数:8页
时间:2019-07-06
《java编程例子源码(狐狸与兔子)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、--------------------------------------------animalpackageanimal;importjava.util.ArrayList;importfield.Location;publicabstractclassAnimal{privateintageLimit;privateintbreedableAge;privateintage;privatebooleanisAlive=true;publicAnimal(intageLimit,intbreedableAge)
2、{this.ageLimit=ageLimit;this.breedableAge=breedableAge;}protectedintgetAge(){returnage;}protecteddoublegetAgePercent(){return(double)age/ageLimit;}publicabstractAnimalbreed();publicvoidgrow(){age++;if(age>=ageLimit){die();}}publicvoiddie(){isAlive=false;}public
3、booleanisAlive(){returnisAlive;}publicbooleanisBreedable(){returnage>=breedableAge;}publicLocationmove(Location[]freeAdj){Locationret=null;if(freeAdj.length>0&&Math.random()<0.02){ret=freeAdj[(int)(Math.random()*freeAdj.length)];}returnret;}@OverridepublicStrin
4、gtoString(){return""+age+":"+(isAlive?"live":"dead");}publicAnimalfeed(ArrayListneighbour){returnnull;}protectedvoidlongerLife(intinc){ageLimit+=inc;}}-----------------------------------------------Cellpackagecell;importjava.awt.Graphics;publicinterface
5、Cell{voiddraw(Graphicsg,intx,inty,intsize);}-----------------------------------------------Locationpackagefield;publicclassLocation{privateintrow;privateintcol;publicLocation(introw,intcol){this.row=row;this.col=col;}publicintgetRow(){returnrow;}publicintgetCol
6、(){returncol;}}-----------------------------------------------Foxpackageanimal;importjava.awt.Color;importjava.awt.Graphics;importjava.util.ArrayList;importcell.Cell;publicclassFoxextendsAnimalimplementsCell{publicFox(){super(20,4);}@Overridepublicvoiddraw(Grap
7、hicsg,intx,inty,intsize){intalpha=(int)((1-getAgePercent())*255);g.setColor(newColor(0,0,0,alpha));//(int)((20-getAge())/20.0*255)));g.fillRect(x,y,size,size);}@OverridepublicAnimalbreed(){Animalret=null;if(isBreedable()&&Math.random()<0.05){ret=newFox();}retur
8、nret;}@OverridepublicStringtoString(){return"Fox:"+super.toString();}@OverridepublicAnimalfeed(ArrayListneighbour){Animalret=null;if(Math.random()<0.2){ret=neighbour
此文档下载收益归作者所有