C#接口 - 我该怎么做?

Jim*_* G. 0 c# interface

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; }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能让Person类实现IWorkPerson,并IHomePerson在C#中?

编译器抱怨含糊不清的引用.

ken*_*nny 5

查看接口的显式实现.

从引用的链接:

// 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());
   }
}
Run Code Online (Sandbox Code Playgroud)


Nic*_*rey 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; }
}
Run Code Online (Sandbox Code Playgroud)