php接口的实现

接口
是一种特殊的抽象类
所有成员属性必须是常量
所有方法都是抽象的
所有成员都必须是public
使用类去实现接口中全部方法
接口跟接口之间和实现类跟接口都用如下
  class Demo implements Use   //使用类去实现接口
  class Demo implements Use1, User2  //接口跟接口之间

 

 

类的访问类型
private         同一个类中的内部成员都是可读、写,子类里都 不可使用
protected     同一个类中的内部成员都是可读、写,子类里都可使用
public             同一个类中的内部成员都是可读、写,  外部都可以访问读、写

 

 

       interface One
       {
           const TEST = "hello";//接口只能是常量

           function fun1();

           function fun2();
       }

       interface Two extends One
       { //接口直接相互继承,但若类继承了后面的接口2,需要把接口1中的方法也实现了
           function fun3();

           function fun4();
       }

       interface Three
       {
           function fun5();
       }

       class Demo implements One
       { //演示普通类实现接口
           function fun1()
           {
               echo "this is fun1()
";
           }

           function fun2()
           {
               echo "this is fun2()
";
           }
       }

       class DemoTwo implements Two
       {//演示类实现   接口的相互继承关系
           function fun1()
           {
               echo "this is DemoTwo fun1()
";
           }

           function fun2()
           {
               echo "this is DemoTwo fun2()
";
           }

           function fun3()
           {
               echo "this is DemoTwo fun3()
";
           }

           function fun4()
           {
               echo "this is DemoTwo fun4()
";
           }
       }

       class DemoThree implements Two, Three
       {//演示类实现  继承多个接口
           function fun1()
           {
               echo "this is DemoThree fun1()
";
           }

           function fun2()
           {
               echo "this is DemoThree fun2()
";
           }

           function fun3()
           {
               echo "this is DemoThree fun3()
";
           }

           function fun4()
           {
               echo "this is DemoThree fun4()
";
           }

           function fun5()
           {
               echo "this is DemoThree fun5()
";
           }
       }

       class Newclass
       {
           function fun6()
           {
               echo "this is Newclass fun6()
";
           }
       }

       class DemoFour extends Newclass implements Two, Three
       {//演示类实现  继承一个类再实现多个接口
           function fun1()
           {
               echo "this is DemoThree fun1()
";
           }

           function fun2()
           {
               echo "this is DemoThree fun2()
";
           }

           function fun3()
           {
               echo "this is DemoThree fun3()
";
           }

           function fun4()
           {
               echo "this is DemoThree fun4()
";
           }

           function fun5()
           {
               echo "this is DemoThree fun5()
";
           }
       }

//
//实现类1 接口直接相互继承,但若类继承了后面的接口2,需要把接口1中的方法也实现了
       $demo = new Demo();

       echo Demo::TEST."
";
       $demo->fun1();
       $demo->fun2();


///
//实现类2  演示类实现  接口的相互继承关系
       $demotwo = new DemoTwo();

       echo Demo::TEST."
";
       $demotwo->fun1();
       $demotwo->fun2();
       $demotwo->fun3();
       $demotwo->fun4();

///
//实现类3  演示类实现  继承多个接口
       $demothree = new DemoThree();

       echo Demo::TEST."
";
       $demothree->fun1();
       $demothree->fun2();
       $demothree->fun3();
       $demothree->fun4();
       $demothree->fun5();

///
//实现类4  演示类实现  继承一个类再实现多个接口
       $demofour = new DemoFour();

 

      echo Demo::TEST."
";
       $demofour->fun1();
       $demofour->fun2();
       $demofour->fun3();
       $demofour->fun4();
       $demofour->fun5();
       $demofour->fun6();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值