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
类实现IWorkPerson
和IHomePerson
?
编译器抱怨含糊不清的引用。
答案 0 :(得分: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());
}
}
答案 1 :(得分: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; }
}
答案 2 :(得分: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; }
}
答案 3 :(得分:1)
class Person : IWorkPerson, IHomePerson
{
public string IWorkPerson.FirstName {get; set; }
public string IHomePerson.FirstName {get; set; }
// etc...
}
这称为显式实现,它是您消除相同界面成员歧义的方式。
答案 4 :(得分:0)
在您的班级中提供IWorkPerson.Foo和IHomePerson.Foo的实现。
我希望那些不是真正的接口。两者都应该从一个公共接口继承,我不理解一个Person是HomePerson和WorkPerson的抽象。听起来它应该是一个枚举,而不是一个接口。