[英]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 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; }
}
如何使Person
類在C#中實現IWorkPerson
和IHomePerson
?
編譯器抱怨引用不明確。
看一下接口的顯式實現 。
從引用的鏈接:
// 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());
}
}
從接口聲明中刪除“ public”修飾符后,即可在.Net 2.0、3.0、3.5或4.0下用w.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; }
}
您必須聲明顯式實現:
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...
}
這稱為顯式實現 ,這是您消除相同接口成員的歧義的方法。
在您的課程中提供IWorkPerson.Foo和IHomePerson.Foo的實現。
我希望這些不是真正的界面。 兩者都應繼承自一個通用接口,並且我不理解表示人員既是HomePerson又是WorkPerson的抽象概念。 聽起來應該是枚舉,而不是接口。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.