各种语言的接口继承

因为工作的原因,下午有个同事问我有关php的接口继承的问题:php里如果遇到多接口的实现,并且有多个接口具有同样的方法名,该如何解决:
我当时毫不犹豫地就说可以,并且脑海里马上出现以下代码:
 1 <? php
 2   interface  A {
 3       public   function  hello();
 4  }
 5  
 6   interface  B {
 7       public   function  hello();
 8  }
 9  
10   class  test_interface  implements  A ,  B
11  {
12       public   function  __contruct()
13      {
14      }
15       public   function  A :: hello()
16      {
17           print ( " A::hello()!<br/>\n " );
18      }
19       public   function  B :: hello()
20      {
21           print ( " B::hello()!<br/>\n " );
22      }
23  }
24  
25   function  do_A(A &   $a )
26  {
27       $a -> hello();
28  }
29 ?>
晚上回到家里测试了这段代码,发现出现运行错误,主要在public function A::hello()和public function B::hello()两个地方。
我马上意识到php命名空间的支持比较弱,马上把代码改了下,类似c++:
 1 <? php
 2   interface  A {
 3       public   function  hello();
 4  }
 5  
 6   interface  B {
 7       public   function  hello();
 8  }
 9  
10   class  test_interface  implements  A ,  B
11  {
12       public   function  __contruct()
13      {
14      }
15       public   function  hello()
16      {
17           print ( " A::hello()!<br/>\n " );
18      }
19       /* *
20      public function B::hello()
21      {
22          print("B::hello()!<br/>\n");
23      }
24       */
25  }
26  
27   function  do_A(A &   $a )
28  {
29       $a -> hello();
30  }
31 ?>
运行后还是出现错误,上网查了下资料, 原来php不支持多接口实现中,方法同名的情况不支持
改正的代码如下:
 1 <? php
 2   interface  A {
 3       public   function  hello();
 4  }
 5  
 6   interface  B {
 7       public   function  hellob();
 8  }
 9  
10   class  test_interface  implements  A ,  B
11  {
12       public   function  __contruct()
13      {
14      }
15       public   function  hello()
16      {
17           print ( " A::hello()!<br/>\n " );
18      }
19      
20       public   function  hellob()
21      {
22           print ( " B::hello()!<br/>\n " );
23      }
24      
25  }
26  
27   function  do_A(A &   $a )
28  {
29       $a -> hello();
30  }
31 ?>

兴致来了,就想回顾下C++对这种情况的支持又如何;于是编写了以下代码:
 1 #include  < iostream >
 2 using   namespace  std;
 3 class  A
 4 {
 5    public:
 6        virtual void functionA() = 0;
 7}
;
 8
 9 class  B
10 {
11    public:
12        virtual void functionA()=0;
13
14class C : public A, public B
15{
16    public:
17        void functionA()
18        {
19            cout << " C implements functionA of A\n";
20        }

21        
22        /**
23        void B::functionA()
24        {
25            cout << " C implements functionA of B\n";
26        }
27        */

28}
;
29
30int test_interface_for_a(A& a)
31{
32    a.functionA();
33}

34
35int test_interface_for_b(B& b)
36{
37    b.functionA();
38}

39
40int main(int argc, char* argv[])
41{
42    A* pa = new C();
43    B* pb = new C();
44    test_interface_for_a(*pa);
45    test_interface_for_b(*pb); 
46}
编译通过,结果如下:
C implements functionA of A
C implements functionA of A
想试试在B类中实现下接口:代码如下:
#include  < iostream >
using   namespace  std;
class  A
{
    
public:
        
virtual void functionA() = 0;
}
;

class  B
{
    
public:
        
virtual void functionA()
        
{
            cout 
<< "B implements functionA\n";
        }

}
;

class  C :  public  A,  public  B
{
    
public:
        
void functionA()
        
{
            cout 
<< " C implements functionA of A\n";
        }

        
        
/**
        void B::functionA()
        {
            cout << " C implements functionA of B\n";
        }
        
*/

}
;

int  test_interface_for_a(A &  a)
{
    a.functionA();
}


int  test_interface_for_b(B &  b)
{
    b.functionA();
}


int  main( int  argc,  char *  argv[])
{
    A
* pa = new C();
    B
* pb = new C();
    test_interface_for_a(
*pa);
    test_interface_for_b(
*pb); 
}

编译通过,并且结果和上面的一样,想了下,觉得是c++多态的结果。有时间看看java是如何支持这种方式的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值