当前位置:网站首页>【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("妈妈");
?>
结果:
接口继承,要实现两个抽象方法
我的名字是:小强
平时经常做饭的人是:妈妈
边栏推荐
- URL和URI的关系
- Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
- pycharm 终端部启用虚拟环境
- 分步式監控平臺zabbix
- [flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
- Pycharm terminal enables virtual environment
- hellogolang
- Shader_ Animation sequence frame
- Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
- Sysom case analysis: where is the missing memory| Dragon lizard Technology
猜你喜欢
[vulnhub range] thales:1
AutoLISP series (3): function function 3
Tragedy caused by deleting the console statement
Xcode Revoke certificate
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
删除 console 语句引发的惨案
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
pycharm 终端部启用虚拟环境
Pycharm terminal enables virtual environment
【Vulnhub靶场】THALES:1
随机推荐
three. JS create cool snow effect
Three. JS series (2): API structure diagram-2
Detailed explanation of several ideas for implementing timed tasks in PHP
Strengthen real-time data management, and the British software helps the security construction of the medical insurance platform
Sysom case analysis: where is the missing memory| Dragon lizard Technology
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
Iptables only allows the specified IP address to access the specified port
What else can an ordinary person do besides working in a factory to make money?
Talk about the cloud deployment of local projects created by SAP IRPA studio
平衡二叉树(AVL)
Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
torch. Numel action
如何快速检查钢网开口面积比是否符合 IPC7525
Enterprise log analysis system elk
Usage of config in laravel
【C 语言】 题集 of Ⅹ
U3D_ Infinite Bessel curve
Laravel service provider instance tutorial - create a service provider test instance
Leetcode-136- number that appears only once (solve with XOR)
Lecturer solicitation order | Apache seatunnel (cultivating) meetup sharing guests are in hot Recruitment!