flyonok

统计

留言簿(7)

ACE

book

boost

bsd

c study

c++

code download

codeblock

computer clound

Eclipse

embed system

erlang

ET++

gtk

ic card

java

KDE

libevent

linux

linux--MM

mysql

network education

one card

oracle

pcap relation

php

powerbuilder

python

QT

software config

software test

SQL server

UML

wireless

wxwidgets

陈宾

阅读排行榜

评论排行榜

各种语言的接口继承

因为工作的原因,下午有个同事问我有关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>
 2using namespace std;
 3class A
 4{
 5    public:
 6        virtual void functionA() = 0;
 7}
;
 8
 9class 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是如何支持这种方式的。

posted on 2010-07-08 00:36 flyonok 阅读(314) 评论(1)  编辑 收藏 引用 所属分类: c++

评论

# re: 各种语言的接口继承 2010-07-09 17:09 陈梓瀚(vczh)

C++的话,同名了就意味着功能应该绝对一样,因此不做区分了。C#你可以选择覆盖一个interface的method之后,他不作为class本身的method出现,从而在一定程度上解决了这个问题。  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理