当前位置:网站首页>[PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
[PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
2022-07-07 16:36:00 【weixin_ forty-three million two hundred and twenty-four thousan】
stay PHP The interface of , Interface can inherit interface . although PHP Class can only inherit one parent class ( Single inheritance ), But interfaces and classes are different , Interfaces can implement multiple inheritance , You can inherit one or more interfaces . Of course, interface inheritance also uses extends keyword , If you want more than one inheritance, just separate the inherited interfaces with commas .
It should be noted that when your interface inherits other interfaces , Directly inherit the static constant properties and abstract methods of the parent interface , So when a class implements an interface, it must implement all the relevant abstract methods .
Now you are right PHP Have you understood the inheritance of interfaces , The following examples are for reference , The code is as follows
<?php
interface father{
function shuchu();
}
interface fam extends father{
function cook($name);
}
class test implements fam{
function shuchu(){
echo " Interface inheritance , To implement two abstract methods ";
echo "<br>";
}
function cook($name){
echo " People who usually cook are :".$name;
}
}
$t=new test();
$t->shuchu();
$t->cook(" Mom ");
?>
result :
Interface inheritance , To implement two abstract methods
People who usually cook are : Mom
The above example is that the interface inherits an interface , So in test Class implementation fam Two abstract methods should be instantiated in the interface , It is to instance the abstract methods of the subclass and parent of the interface .
Let's take a look at an example of interface multi inheritance , The code is as follows :
<?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 name is :".$my;
echo "<br>";
}
function shuchu(){
echo " Interface inheritance , To implement two abstract methods ";
echo "<br>";
}
function cook($name){
echo " People who usually cook are :".$name;
}
}
$t=new test();
$t->shuchu();
$t->dayin(" cockroach ");
$t->cook(" Mom ");
?>
result :
Interface inheritance , To implement two abstract methods
My name is : cockroach
People who usually cook are : Mom
边栏推荐
- 预测——灰色预测
- 47_ Contour lookup in opencv cv:: findcontours()
- PHP has its own filtering and escape functions
- Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition
- laravel构造函数和中间件执行顺序问题
- MySQL数据库基本操作-DQL-基本查询
- Inner monologue of accidental promotion
- 【DesignMode】外观模式 (facade patterns)
- Vs tool word highlight with margin
- PHP实现执行定时任务的几种思路详解
猜你喜欢
使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
3000 words speak through HTTP cache
统计学习方法——感知机
Good news! Kelan sundb database and Hongshu technology privacy data protection management software complete compatibility adaptation
[C language] question set of X
【DesignMode】外观模式 (facade patterns)
模仿企业微信会议室选择
The difference and working principle between compiler and interpreter
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
Personal notes of graphics (2)
随机推荐
OpenGL personal notes
【医学分割】attention-unet
面试题 01.02. 判定是否互为字符重排-辅助数组算法
JS 模块化
C语言进阶——函数指针
Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench
What about the pointer in neural network C language
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
【Android -- 数据存储】使用 SQLite 存储数据
Prometheus API deletes all data of a specified job
You Yuxi, coming!
爬虫(17) - 面试(2) | 爬虫面试题库
JS中null NaN undefined这三个值有什么区别
Laravel5.1 路由 -路由分组
修改配置文件后tidb无法启动
华东师大团队提出,具有DNA调控电路的卷积神经网络的系统分子实现
记录Servlet学习时的一次乱码
Three. JS series (2): API structure diagram-2
【DesignMode】代理模式(proxy pattern)
js中复选框checkbox如何判定为被选中