当前位置:网站首页>【PHP】PHP接口继承及接口多继承原理与实现方法
【PHP】PHP接口继承及接口多继承原理与实现方法
2022-07-07 14:16:00 【weixin_43224306】
在PHP的接口中,接口可以继承接口。虽然PHP类只能继承一个父类(单继承),但是接口和类不同,接口可以实现多继承,可以继承一个或者多个接口。当然接口的继承也是使用extends关键字,要多个继承的话只要用逗号把继承的接口隔开即可。
需要注意的是当你接口继承其它接口时候,直接继承父接口的静态常量属性和抽象方法,所以类实现接口时必须实现所有相关的抽象方法。
现在你对PHP接口的继承有所了解了吧,下面的例子可供参考,代码如下
<?php
interface father{
function shuchu();
}
interface fam extends father{
function cook($name);
}
class test implements fam{
function shuchu(){
echo "接口继承,要实现两个抽象方法";
echo "<br>";
}
function cook($name){
echo "平时经常做饭的人是:".$name;
}
}
$t=new test();
$t->shuchu();
$t->cook("妈妈");
?>
结果:
接口继承,要实现两个抽象方法
平时经常做饭的人是:妈妈
上面的示例是接口继承了一个接口,所以在test类实现fam接口的时候要实例两个抽象方法,就是把接口的子类和父类的抽象方法都实例。
下面来看一个接口多继承的示例,代码如下:
<?php
interface father{
function shuchu();
}
interface mother{
function dayin($my);
}
interface fam extends father,mother{
function cook($name);
}
class test implements fam{
function dayin($my){
echo "我的名字是:".$my;
echo "<br>";
}
function shuchu(){
echo "接口继承,要实现两个抽象方法";
echo "<br>";
}
function cook($name){
echo "平时经常做饭的人是:".$name;
}
}
$t=new test();
$t->shuchu();
$t->dayin("小强");
$t->cook("妈妈");
?>
结果:
接口继承,要实现两个抽象方法
我的名字是:小强
平时经常做饭的人是:妈妈
边栏推荐
- Rongyun won the 2022 China Xinchuang digital office portal excellence product award!
- Asyncio concept and usage
- The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
- [C language] question set of X
- Introduction to ThinkPHP URL routing
- Continuous creation depends on it!
- 企业级日志分析系统ELK
- Regular expression string
- three.js打造酷炫下雪效果
- 分步式监控平台zabbix
猜你喜欢

Continuous creation depends on it!

95. (cesium chapter) cesium dynamic monomer-3d building (building)

Have fun | latest progress of "spacecraft program" activities

SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术

Plate - forme de surveillance par étapes zabbix

Talk about the cloud deployment of local projects created by SAP IRPA studio

What about the pointer in neural network C language

The unity vector rotates at a point

AutoLISP series (1): function function 1

Three. JS series (2): API structure diagram-2
随机推荐
Laravel5.1 路由 -路由分组
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
Particle effect for ugui
分步式监控平台zabbix
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
95. (cesium chapter) cesium dynamic monomer-3d building (building)
Prometheus API deletes all data of a specified job
[excelexport], Excel to Lua, JSON, XML development tool
What is the difference between IP address and physical address
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
js中复选框checkbox如何判定为被选中
【Android -- 数据存储】使用 SQLite 存储数据
Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!
Unity3d click events added to 3D objects in the scene
Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
Imitate the choice of enterprise wechat conference room
Description of vs common shortcut keys
Application example of infinite list [uigridview]
Continuous creation depends on it!
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题