2013-05-11 74 views

回答

6

你的接口都不会编译,我假设它们是方法而不是字段。

实现与冲突的membernames多个接口的唯一方法是通过使用显式实现:

interface a1 
{ 
    int mycount(); 
} 

interface a2 
{ 
    string mycount(); 
} 


class Foo : a1, a2 
{ 
    int a1.mycount()  { ... } 
    string a2.mycount() { ... } 


    // you can _only_ access them through an interface reference 
    // even Bar members need to typecast 'this' to call these methods 
    void Bar() 
    { 
     var x = mycount();    // Error, won't compile 
     var y = (this as a2).mycount(); // Ok, y is a string 
    } 
} 
+0

谢谢Henk。这是假设您已将数据成员更改为方法,并且我知道此实现将起作用。 我的问题是,在最近的一次采访中,我遇到了同样的问题,并且我的答案被拒绝了 - 来自不同界面的同名数据成员不可能继承到同一个类中。所以我想确认这是正确的。面试官因为某种原因认为不然,这让我感到十分困惑。 – Alag20 2013-05-11 09:53:55

相关问题