多态性是指相同的操作或函数、过程可作用于多种类型的对象上并获得不同的结果。不同的对象,收到同一消息将可以产生不同的结果,这种现象称为多态性。
多态性允许每个对象以适合自身的方式去响应共同的消息。多态性增强了软件的灵活性和重用性。
1.应用多态性
例:poly.php
class Fruit{//创建Fruit类
protected $color; //保护 成员变量 $color 可被继承
public function __construct(){ // 公有 构造函数
echo 'This is fruit.
'; //打印This is fruit.
}
public function grow(){ //公有 成员函数 grow 所有水果的共同行为
}
}
class Apple extends Fruit{ //创建Apple类 父类Fruit
public function __construct(){ //公有 构造函数
$this->color = 'red';//继承父类color属性
echo 'This is apple.
';//打印This is apple.
}
public function grow(){ //公有 成员函数 苹果、香蕉、桔子生长行为各不相同,它们有不同的实现方式
echo 'Apple grown mothed.
';//打印Apple grown mothed.
}
}
class Banana extends Fruit{ //创建Banana类 父类Fruit
public function __construct(){ //公有 构造函数
$this->color = 'yelow';//继承父类color属性
echo 'This is banana.
';//打印This is banana.
}
public function grow(){//公有 成员函数
echo 'Banana grown mothed.
';//打印Banana grown mothed.
}
}
class Orange extends Fruit{//创建Orange类 父类Fruit
public function __construct(){//公有 构造函数
$this->color = 'green';//继承父类color属性
echo 'This is orange.
';//打印This is orange.
}
public function grow(){//公有 成员函数
echo 'Orange grown mothed.
';//打印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
class Fruit{//创建Fruit类
protected $color;//保护 成员变量 $color 可被继承
public function __construct(){// 公有 构造函数
echo 'This is fruit.
';//打印This is fruit.
}
public function grow(){ //公有 成员函数 grow 所有水果的共同行为
}
}
class Apple extends Fruit{//创建Apple类 父类Fruit
public function __construct(){ //公有 构造函数
$this->color = 'red';//继承父类color属性
echo 'This is apple.
';//打印This is apple.
}
public function grow(){//公有 成员函数
echo 'Apple grown mothed.
';//打印Apple grown mothed.
}
}
class Banana extends Fruit{//创建Apple类 父类Fruit
public function __construct(){//公有 构造函数
$this->color = 'yelow';//继承父类color属性
echo 'This is banana.
';//打印This is banana.
}
public function grow(){//公有 成员函数
echo 'Banana grown mothed.
';//打印Banana grown mothed.
}
}
class Orange extends Fruit{//创建Orange类 父类Fruit
public function __construct(){//公有 构造函数
$this->color = 'green';//继承父类color属性
echo 'This is orange.
';//打印This is orange.
}
public function grow(){//公有 成员函数
echo 'Orange grown mothed.
';//打印Orange grown mothed.
}
}
function getFruit(Fruit $fruit){ //创建getFruit函数
if($fruit instanceof Apple) //instanceof 用于确定一个 $fruit 变量是否属于某一类 Apple 的实例:
echo 'Apple类
'; //打印Apple类
else if($fruit instanceof Banana)//instanceof 用于确定一个 $fruit 变量是否属于某一类 Banana 的实例:
echo 'Banana类
';//打印Banana类
else if($fruit instanceof Orange)//instanceof 用于确定一个 $fruit 变量是否属于某一类 Orange的实例:
echo 'Orange类
';//打印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()
?>
结果:
请大家多多关注!谢谢,本文只供学习之用。