当前位置:网站首页>【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("妈妈");
?>
结果:
接口继承,要实现两个抽象方法
我的名字是:小强
平时经常做饭的人是:妈妈
边栏推荐
- 121. 买卖股票的最佳时机
- 【HCSD大咖直播】亲授大厂面试秘诀-简要笔记
- spark调优(三):持久化减少二次查询
- Performance measure of classification model
- prometheus api删除某个指定job的所有数据
- Prediction - Grey Prediction
- Xingruige database was shortlisted as the "typical solution for information technology application and innovation in Fujian Province in 2021"
- Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
- MySQL数据库基本操作-DQL-基本查询
- PHP中exit,exit(0),exit(1),exit(‘0’),exit(‘1’),die,return的区别
猜你喜欢
Xcode Revoke certificate
无法将“pip”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
谈谈 SAP iRPA Studio 创建的本地项目的云端部署问题
【C 语言】 题集 of Ⅹ
Record the migration process of a project
AutoLISP series (2): function function 2
Prediction - Grey Prediction
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
如何快速检查钢网开口面积比是否符合 IPC7525
3000 words speak through HTTP cache
随机推荐
What about the pointer in neural network C language
Talk about the cloud deployment of local projects created by SAP IRPA studio
How to query the data of a certain day, a certain month, and a certain year in MySQL
Prediction - Grey Prediction
Performance measure of classification model
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
Common training data set formats for target tracking
[hcsd celebrity live broadcast] teach the interview tips of big companies in person - brief notes
Tragedy caused by deleting the console statement
js中复选框checkbox如何判定为被选中
logback.xml配置不同级别日志,设置彩色输出
Set the route and optimize the URL in thinkphp3.2.3
Laravel5.1 路由 -路由分组
Unity3d click events added to 3D objects in the scene
prometheus api删除某个指定job的所有数据
Cesium(3):ThirdParty/zip. js
Logback logging framework third-party jar package is available for free
23. 合并K个升序链表-c语言
01tire+ chain forward star +dfs+ greedy exercise one
thinkphp3.2.3中设置路由,优化url