php不调用父类构造_PHP 多态学习笔记

多态性是指相同的操作或函数、过程可作用于多种类型的对象上并获得不同的结果。不同的对象,收到同一消息将可以产生不同的结果,这种现象称为多态性。

多态性允许每个对象以适合自身的方式去响应共同的消息。多态性增强了软件的灵活性和重用性。

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()

?>

结果:

ac1dbe88f4c4ff2a0a32c730ed683a7d.png

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()

?>

结果:

22adcdb984711dced842158040e59ab4.png

请大家多多关注!谢谢,本文只供学习之用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值