<? PHP interface computer {public function version (); Public Function work ();} class notecomputer implements computer {public function version () {echo 'notebook ';} public function work () {echo 'can run win7 in a portable manner !! ';}} Class extends topcomputer implements computer {public function version () {echo' desktop! ';} Public function work () {echo' runs XP on the workstation! ';}} Class person {public function _ run ($ type) {echo 'this person's'; $ type-> Version (); $ type-> Work () ;}/// the principle of polymorphism is that all classes have been written ~ Do not modify it ~ As long as the call parameter changes outside the class, // the final result will also be modified, then this is the polymorphism $ notecomputer = new notecomputer (); $ your topcomputer = new your topcomputer (); $ person = new person (); $ person-> _ run ($ notecomputer); // object reference transfer, holding the echo '<br/>' of the other party '; $ person-> _ run ($ your topcomputer);?>
Small examples of Polymorphism