php--class多态性的实现方式
发布日期:2021-05-06 21:09:04 浏览次数:22 分类:技术文章

本文共 1003 字,大约阅读时间需要 3 分钟。

"; }}class tomato_vegetable extends vegetables02{ public function go_vegetable() { echo "我是西红柿,我是西红柿,既是水果,也是蔬菜。"."
"; }}function change($obj){ if($obj instanceof vegetables02) { $obj->go_vegetable(); } else { echo "输入的参数不是vegetables02的类型."."
"; }}echo "实例化potapo:";change(new potato_vegetable());echo "

";echo "实例化tomato:";change(new tomato_vegetable());echo "
";//interface接口实现多态性interface go_vegetables{ public function go_vegetables();}class potato_v implements go_vegetables{ public function go_vegetables() { echo "我说我是土豆,你信不?"."
"; }}class tomato_v implements go_vegetables{ public function go_vegetables() { echo "我说我是西红柿,你可以信。"."
"; }}function change_01($obj){ if($obj instanceof go_vegetables) { $obj->go_vegetables(); } else { echo "输入的参数不是interface类型"."
"; }}echo "实例化potato:";change_01(new potato_v());echo "

";echo "实例化tomato:";change_01(new tomato_v());echo "

";

 

上一篇:php-异常处理die的用法
下一篇:php --interface接口的使用方法

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年04月07日 21时07分40秒