2011-08-22 56 views
0
Interface IWorkPerson 
{ 
    public string FirstName {get; set;} 
    public string LastName { get; set; } 
    public string WorkPhone { get; set; } 
} 

interface IHomePerson 
{ 
    public string FirstName {get; set;} 
    public string LastName { get; set; } 
    public string HomePhone { get; set; } 
} 

public class Person : IWorkPerson, IHomePerson 
{ 
    public string FirstName {get; set;} 
    public string LastName { get; set; } 
    public string WorkPhone { get; set; } 
    public string HomePhone { get; set; } 
} 

如何使C#中的Person类实现IWorkPersonIHomePersonC#接口 - 我该如何做到这一点?

编译器会抱怨不明确的引用。

+2

此代码不能编译,*公众*是不是在接口声明中无效。这是另外定义好的,没有“模棱两可的参考”。发布实际产生错误的代码。 –

+0

请提供编译代码(即删除接口属性公共)和reporoduces错误 - 没有错具有相同的方法/属性是多接口的一部分... –

+0

作品在VS2010(从界面移除“公共”之后属性) – devio

回答

5

一旦你从你的接口声明剥离出“公共”修饰符编译的.Net 2.0,3.0,3.5或4.0以下罚款瓦特/ VS2010。你使用的是什么版本的Visual Studio(或者你在使用Mono?)以及你定位的.Net框架的版本是什么?

此代码编译绝对干净:

public interface IWorkPerson 
{ 
    string FirstName { get; set; } 
    string LastName { get; set; } 
    string WorkPhone { get; set; } 
} 
public interface IHomePerson 
{ 
    string FirstName { get; set; } 
    string LastName { get; set; } 
    string HomePhone { get; set; } 
} 
public class Person : IWorkPerson , IHomePerson 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string WorkPhone { get; set; } 
    public string HomePhone { get; set; } 
} 
5

explicit implementation of intefaces

从引用链接:

// explicit1.cs 
interface IDimensions 
{ 
    float Length(); 
    float Width(); 
} 

class Box : IDimensions 
{ 
    float lengthInches; 
    float widthInches; 

    public Box(float length, float width) 
    { 
     lengthInches = length; 
     widthInches = width; 
    } 
    // Explicit interface member implementation: 
    float IDimensions.Length() 
    { 
     return lengthInches; 
    } 
    // Explicit interface member implementation: 
    float IDimensions.Width() 
    { 
     return widthInches;  
    } 

    public static void Main() 
    { 
     // Declare a class instance "myBox": 
     Box myBox = new Box(30.0f, 20.0f); 
     // Declare an interface instance "myDimensions": 
     IDimensions myDimensions = (IDimensions) myBox; 
     // Print out the dimensions of the box: 
     /* The following commented lines would produce compilation 
     errors because they try to access an explicitly implemented 
     interface member from a class instance:     */ 
     //System.Console.WriteLine("Length: {0}", myBox.Length()); 
     //System.Console.WriteLine("Width: {0}", myBox.Width()); 
     /* Print out the dimensions of the box by calling the methods 
     from an instance of the interface:       */ 
     System.Console.WriteLine("Length: {0}", myDimensions.Length()); 
     System.Console.WriteLine("Width: {0}", myDimensions.Width()); 
    } 
} 
2

您必须声明明确实现:

public class Person : IWorkPerson, IHomePerson 
{ 
    public string IWorkPerson.FirstName {get; set;} 
    public string IWorkPerson.LastName { get; set; } 
    public string IHomePerson.FirstName {get; set;} 
    public string IHomePerson.LastName { get; set; } 
    public string WorkPhone { get; set; } 
    public string HomePhone { get; set; } 
} 
0

在你的类提供IWorkPerson.Foo和IHomePerson.Foo实现。

我希望那些都不是真正的接口。两者都应该从一个通用接口继承,并且我不理解这样的抽象:一个人既是一个主人,又是一个工作人员。听起来像它应该是一个枚举,而不是一个接口。

+0

这是一个完全捏造的例子,因为我在工作中有一个更复杂的继承问题。 –

+0

@Jim G .:那么也许你应该向我们展示导致问题的代码。你的示例代码是完全合法的。 –

1
class Person : IWorkPerson, IHomePerson 
{ 
    public string IWorkPerson.FirstName {get; set; } 
    public string IHomePerson.FirstName {get; set; } 
    // etc... 
} 

这就是所谓的明确的实施,它是你如何消除歧义相同的接口成员。