开发者

C# Interfaces - How Can I Do This?

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开发者_StackOverflow中文版 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; }
}

How can I make the Person class implement IWorkPerson and IHomePerson in C#?

The compiler complains about ambiguous references.


Compiles fine w/VS2010 under .Net 2.0, 3.0, 3.5 or 4.0 once you strip out the 'public' modifiers from your interface declarations. What version of Visual Studio are you using (or are you using Mono?) and what version of the .Net framework are you targeting?

This code compiles absolutely clean:

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; }
}


Look at explicit implementation of intefaces.

From the referenced link:

// 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());
   }
}


You have to declare explicit implementations:

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; }
}


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

This is called explicit implementation and it's how you disambiguate identical interface members.


Provide implementations for IWorkPerson.Foo and IHomePerson.Foo in your class.

I hope those aren't real interfaces. Both should inherit from a common interface, and I don't understand the abstraction that says that a Person is both a HomePerson and a WorkPerson. Sounds like it should be an enumeration, not an interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜