多态性许可每个工具以适宜自身的办法去相应共同的。多态性增强了软件的灵巧性和重用性。
1.运用多态性
例:poly.php

<?php
class Fruit{//创建Fruit类
protected $color; //保护 成员变量 $color 可被继续
public function __construct(){ // 公有 布局函数
echo 'This is fruit.<br><br>'; //打印This is fruit.
}
public function grow(){ //公有 成员函数 grow 所有水果的共同行为
}
}
class Apple extends Fruit{ //创建Apple类 父类Fruit
public function __construct(){ //公有 布局函数
$this->color = 'red';//继续父类color属性
echo 'This is apple.<br><br>';//打印This is apple.
}
public function grow(){ //公有 成员函数 苹果、喷鼻香蕉、桔子成长行为各不相同,它们有不同的实现办法
echo 'Apple grown mothed.<br><br>';//打印Apple grown mothed.
}
}
class Banana extends Fruit{ //创建Banana类 父类Fruit
public function __construct(){ //公有 布局函数
$this->color = 'yelow';//继续父类color属性
echo 'This is banana.<br><br>';//打印This is banana.
}
public function grow(){//公有 成员函数
echo 'Banana grown mothed.<br><br>';//打印Banana grown mothed.
}
}
class Orange extends Fruit{//创建Orange类 父类Fruit
public function __construct(){//公有 布局函数
$this->color = 'green';//继续父类color属性
echo 'This is orange.<br><br>';//打印This is orange.
}
public function grow(){//公有 成员函数
echo 'Orange grown mothed.<br><br>';//打印Orange grown mothed.
}
}
$apple = new Apple();//实例化
$banana = new Banana();//实例化
$orange = new Orange();//实例化
$apple->grow();//调用apple的grow()
$banana->grow();//调用banana的grow()
$orange->grow();//调用orange的grow()
?>
结果:
PHP供应了instanceof关键字来判断当前工具的类型,因此我们也可以出当前的工具是什么形态的工具,它属于什么类型。
例:poly2.php
<?php
class Fruit{//创建Fruit类
protected $color;//保护 成员变量 $color 可被继续
public function __construct(){// 公有 布局函数
echo 'This is fruit.<br><br>';//打印This is fruit.
}
public function grow(){ //公有 成员函数 grow 所有水果的共同行为
}
}
class Apple extends Fruit{//创建Apple类 父类Fruit
public function __construct(){ //公有 布局函数
$this->color = 'red';//继续父类color属性
echo 'This is apple.<br><br>';//打印This is apple.
}
public function grow(){//公有 成员函数
echo 'Apple grown mothed.<br><br>';//打印Apple grown mothed.
}
}
class Banana extends Fruit{//创建Apple类 父类Fruit
public function __construct(){//公有 布局函数
$this->color = 'yelow';//继续父类color属性
echo 'This is banana.<br><br>';//打印This is banana.
}
public function grow(){//公有 成员函数
echo 'Banana grown mothed.<br><br>';//打印Banana grown mothed.
}
}
class Orange extends Fruit{//创建Orange类 父类Fruit
public function __construct(){//公有 布局函数
$this->color = 'green';//继续父类color属性
echo 'This is orange.<br><br>';//打印This is orange.
}
public function grow(){//公有 成员函数
echo 'Orange grown mothed.<br><br>';//打印Orange grown mothed.
}
}
function getFruit(Fruit $fruit){ //创建getFruit函数
if($fruit instanceof Apple) //instanceof 用于确定一个 $fruit 变量是否属于某一类 Apple 的实例:
echo 'Apple类<br><br>'; //打印Apple类
else if($fruit instanceof Banana)//instanceof 用于确定一个 $fruit 变量是否属于某一类 Banana 的实例:
echo 'Banana类<br><br>';//打印Banana类
else if($fruit instanceof Orange)//instanceof 用于确定一个 $fruit 变量是否属于某一类 Orange的实例:
echo 'Orange类<br><br>';//打印Orange类
else
echo 'unknown'; //打印unknown
}
$apple = new Apple();//实例化
$banana = new Banana();//实例化
$orange = new Orange();//实例化
getFruit($banana );//调用getFruit函数
$apple->grow();//调用apple的grow()
$banana->grow();//调用banana的grow()
$orange->grow();//调用orange的grow()
?>
结果:
请大家多多关注!
感激,本文只供学习之用。