没有写停止条件,所以会一直运行哟。
<?php /* The template design pattern defines the program skeleton of an algorithm in a method. It lets us, via use of class overriding, redefine certain steps of an algorithm without really changing the algorithm's structure. */ abstract class Game { private $playerCount; abstract function initializeGame(); abstract function makePlay($player); abstract function endOfGame(); abstract function printWinner(); public function playOneGame($playerCount) { $this->playerCount = $playerCount; $this->initializeGame(); $j = 0; while (!$this->endOfGame()) { $this->makePlay($j); $j = ($j + 1) % $playerCount; } $this->printWinner(); } } class Monopoly extends Game { public function initializeGame() { echo 'Monopoly_initializeGame.<br/>'; } public function makePlay($player) { echo 'Monopoly_makePlay.<br/>'; } public function endOfGame(){ echo 'Monopoly_endOfGame.<br/>'; } public function printWinner(){ echo 'Monopoly_printWinner.<br/>'; } } class Chess extends Game { public function initializeGame() { echo 'Chess_initializeGame.<br/>'; } public function makePlay($player) { echo 'Chess_makePlay.<br/>'; } public function endOfGame(){ echo 'Chess_endOfGame.<br/>'; } public function printWinner(){ echo 'Chess_printWinner.<br/>'; } } $game = new Chess(); $game->playOneGame(2); $game = new Monopoly(); $game->playOneGame(4); ?>