php面向对象的多态

  多态是指使用类的上下文来重新定义或改变类的性质或行为,或者说接口的多种不同的实现方式即为多态。把不同的子类对象都当成父类来看,可以屏蔽不同子类对象之间的差异,写出通用的代码,做出通用的编程,以适应需要的不用变化。

 1 interface Computer{    
 2     public function version();
 3     public function work();
 4 }
 5 
 6 class NotebookComputer implements Computer {
 7     public function version(){
 8         echo '联想';
 9     }
10     public function work(){
11         echo '笔记本';
12     }
13 }
14 
15 class desktopComputer implements Computer { 
16     public function version(){
17         echo 'IBM';
18     }
19     public function work(){
20         echo '台式电脑正在运行中!';
21     }
22 }    
23 
24 class Person{
25     public function run($type){
26         $type->version();
27         $type->work();
28     }
29 }
30 $person = new Person();
31 $desktopcomputer = new desktopComputer();
32 $notebookcomputer = new NotebookComputer();
33 $person->run($notebookcomputer);
原文地址:https://www.cnblogs.com/changshaoshuai/p/3285530.html