编辑:
不应有所作为 ,除非您的类实现具有相同属性的两个接口,否则您不应该这样做,因为在访问成员之前,您必须强制转换为相关接口:
public interface ITest
{
string Id { get; }
}
public interface IAlsoTest
{
string Id { get; }
}
public interface ITestToo
{
int Id { get; }
}
public class Test : ITest, IAlsoTest
{
// Valid implicit implementation of BOTH interfaces
public string Id
{
get { throw new NotImplementedException(); }
}
}
public class TestSeparately : ITest, ITestToo
{
// This way we can do different things depending
// on which interface the callee called from.
string ITest.Id
{
get { throw new NotImplementedException(); }
}
int ITestToo.Id
{
get { throw new NotImplementedException(); }
}
}
public class TestOuch
{
public void DoStuff()
{
var ts = new TestSeparately();
// Works
Console.WriteLine(((ITest)ts).Id);
// Works
Console.WriteLine(((ITestToo)ts).Id);
// Not valid! Which one did we want to call?
Console.WriteLine(ts.Id);
}
}
即使您仅使用单个接口(我总是忘记了:S),当您显式实现接口成员时,示例用法仍然适用,因此,我将尽可能避免使用显式实现,因为如果它们隐瞒了类成员,它将隐藏类成员。不要转换为正确的界面(这非常令人困惑)。