用简单的动物类演示php interface接口用法

 刘管家 发表于 2021年02月02日 PHP  730
刘管家
LV 2 [幼儿]
最后在线:3年前
加入时间:4年前
主帖:20  跟帖:1
<?php
#接口的简单理解与使用
#在我对java的理解中接口就是定义常量变量与方法的接口然后加以实现

#定义一个接口动物类动物有爱吃的食物,与吃的方法
interface animal
{
    const foot = '骨头';//定义一个食物常量

    public function eat();//定义一个吃的方法这里注意不能加大括号
}

#定义一个类实现接口中的方法
#狗类也是动物也会吃所以可以继承接口实现方法
class dog implements animal
{
    public function eat()
    {
       echo '我是只狗狗我爱吃'.self::foot;
    }
}

#实例化dog

$wangcai=new dog();

$wangcai->eat();
#输出结果:我是只狗狗我爱吃骨头
论坛主帖
20
论坛跟帖
1
在线访客
0
今日主帖
0
今日跟帖
0
今日注册
0
17
http://m.xn--2brz17g.cn/bbs/gentie.html
http://m.xn--2brz17g.cn/bbs/postzan.html
http://m.xn--2brz17g.cn/bbs/postcai.html
http://m.xn--2brz17g.cn/bbs/postshoucang.html
http://m.xn--2brz17g.cn/bbs/gentiezan.html
http://m.xn--2brz17g.cn/bbs/gentiecai.html
http://m.xn--2brz17g.cn/bbs/huifu.html
http://m.xn--2brz17g.cn/bbs/xiugai.html
http://m.xn--2brz17g.cn/bbs/shanchugentie.html
1
bGF0ZXN0XzA=
0
© 2024 自由园 版权所有
Powered by JianYuLunTan
鲁ICP备16000743号
http://m.xn--2brz17g.cn/bbs/feedback.html
http://m.xn--2brz17g.cn/bbs/qiandao.html
http://m.xn--2brz17g.cn/bbs/denglu.html
0
http://m.xn--2brz17g.cn/bbs/adenglu.html
剑鱼论坛