当前位置:网站首页>[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
边栏推荐
猜你喜欢

使用JSON.stringify()去实现深拷贝,要小心哦,可能有巨坑
作为Android开发程序员,android高级面试

1亿单身男女“在线相亲”,撑起130亿IPO

Shandong old age Expo, 2022 China smart elderly care exhibition, smart elderly care and aging technology exhibition

水平垂直居中 方法 和兼容

Logback日志框架第三方jar包 免费获取

爬虫(17) - 面试(2) | 爬虫面试题库

Opencv personal notes

You Yuxi, coming!

Multiplication in pytorch: mul (), multiply (), matmul (), mm (), MV (), dot ()
随机推荐
【DesignMode】享元模式(Flyweight Pattern)
[Android -- data storage] use SQLite to store data
95. (cesium chapter) cesium dynamic monomer-3d building (building)
Xcode Revoke certificate
模拟Servlet的本质
What is the difference between IP address and physical address
Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
How can laravel get the public path
Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
How to implement backspace in shell
TiDB For PostgreSQL和YugabyteDB在Sysbench上的性能对比
How to determine whether the checkbox in JS is selected
Tidb cannot start after modifying the configuration file
Three. JS series (1): API structure diagram-1
作为Android开发程序员,android高级面试
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
php 自带过滤和转义函数
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
HAVE FUN | “飞船计划”活动最新进展
删除 console 语句引发的惨案