造价通

反馈
取消

热门搜词

造价通

取消 发送 反馈意见

模板方法用例(Java)

2022/07/15159 作者:佚名
导读:/***Anabstractclassthatiscommontoseveralgamesin*whichplayersplayagainsttheothers,butonlyoneis*playingatagiventime.*/abstractclassGame{privateintplayersCount;abstractvoidinitializeGame();abstractvoidma

/***Anabstractclassthatiscommontoseveralgamesin*whichplayersplayagainsttheothers,butonlyoneis*playingatagiventime.*/abstractclassGame{privateintplayersCount;abstractvoidinitializeGame();abstractvoidmakePlay(intplayer);abstractbooleanendOfGame();abstractvoidprintWinner();/*Atemplatemethod:*/finalvoidplayOneGame(intplayersCount){this.playersCount=playersCount;initializeGame();intj=0;while(!endOfGame()){makePlay(j);j=(j 1)%playersCount;}printWinner();}}//Nowwecanextendthisclassinordertoimplementactualgames:classMonopolyextendsGame{/*Implementationofnecessaryconcretemethods*/voidinitializeGame(){//...}voidmakePlay(intplayer){//...}booleanendOfGame(){//...}voidprintWinner(){//...}/*SpecificdeclarationsfortheMonopolygame.*///...}classChessextendsGame{/*Implementationofnecessaryconcretemethods*/voidinitializeGame(){//...}voidmakePlay(intplayer){//...}booleanendOfGame(){//...}voidprintWinner(){//...}/*Specificdeclarationsforthechessgame.*///...}publicclassPlayer{publicstaticvoidmain(String[]args){GamechessGame=newChess();chessGame.initializeGame();chessGame.playOneGame(1);//calltemplatemethod}}

*文章为作者独立观点,不代表造价通立场,除来源是“造价通”外。
关注微信公众号造价通(zjtcn_Largedata),获取建设行业第一手资讯

热门推荐

相关阅读