我有這樣的事情:接口方面採用通用的覆蓋
public class ImplicitClientA : IGenericItems<IGenericItemA>, IGenericItems<IGenericItemB>
public class ImplicitClientB : ImplicitClientA, IGenericItems<IGenericItemC>
,我想重寫IGenericItems<IGenericItemA>
和IGenericItems<IGenericItemB>
在ImplicitClientB
類。
哪一個是最好的方法?
- 要創建
ImplicitClientA
一個虛擬的保護方法和 覆蓋它在ImplicitClientB
。或... - 要明確執行
IGenericItems<IGenericItemA>
和IGenericItems<IGenericItemB>
在類ImplicitClientB
。
下面是代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharpConsoleApplication.Tests
{
public class InterfacesTest
{
public static void Run()
{
var implA = new ImplicitClientA();
IGenericItems<IGenericItemA> implA1 = implA;
IGenericItems<IGenericItemB> implA2 = implA;
Console.WriteLine("Text value for a is: " + implA1.Items.ToList()[0].GetTextValue());
Console.WriteLine("Number value for a is: " + implA2.Items.ToList()[0].GetNumberValue());
Console.WriteLine();
var implB = new ImplicitClientB();
IGenericItems<IGenericItemA> implB1 = implB;
IGenericItems<IGenericItemB> implB2 = implB;
IGenericItems<IGenericItemC> implB3 = implB;
Console.WriteLine("Text value for b is: " + implB1.Items.ToList()[0].GetTextValue());
Console.WriteLine("Number value for b is: " + implB2.Items.ToList()[0].GetNumberValue());
Console.WriteLine("Date value for b is: " + implB3.Items.ToList()[0].GetDateValue());
Console.WriteLine();
}
}
// Interfaces
public interface IGenericItem
{
int Id { get; set; }
}
public interface IGenericItemA : IGenericItem
{
string GetTextValue();
}
public interface IGenericItemB : IGenericItem
{
int GetNumberValue();
}
public interface IGenericItemC : IGenericItem
{
DateTime GetDateValue();
}
public interface IGenericItems<T>
{
IEnumerable<T> Items { get; }
}
// Classes
public class ConcreteA : IGenericItemA
{
public ConcreteA(int id)
{
Id = id;
}
#region IGenericItemA Members
public string GetTextValue()
{
return Id.ToString();
}
#endregion
#region IGenericItem Members
public int Id { get; set; }
#endregion
}
public class ConcreteB : IGenericItemB
{
public ConcreteB(int id)
{
Id = id;
}
#region IGenericItemB Members
public int GetNumberValue()
{
return Id + 1;
}
#endregion
#region IGenericItem Members
public int Id { get; set; }
#endregion
}
public class ConcreteC : IGenericItemC
{
public ConcreteC(int id)
{
Id = id;
}
#region IGenericItemC Members
public DateTime GetDateValue()
{
return DateTime.Now;
}
#endregion
#region IGenericItem Members
public int Id { get; set; }
#endregion
}
// Implicit interfaces implementation
public class ImplicitClientA : IGenericItems<IGenericItemA>, IGenericItems<IGenericItemB>
{
#region IGenericItems<IGenericItemA> Members
public IEnumerable<IGenericItemA> Items
{
get
{
var collection = new List<IGenericItemA>();
collection.Add(new ConcreteA(1));
return collection;
}
}
#endregion
#region IGenericItems<IGenericItemB> Members
IEnumerable<IGenericItemB> IGenericItems<IGenericItemB>.Items
{
get
{
var collection = new List<IGenericItemB>();
collection.Add(new ConcreteB(1));
return collection;
}
}
#endregion
}
public class ImplicitClientB : ImplicitClientA, IGenericItems<IGenericItemC>
{
#region IGenericItems<IGenericItemC> Members
public new IEnumerable<IGenericItemC> Items
{
get
{
var collection = new List<IGenericItemC>();
collection.Add(new ConcreteC(2));
return collection;
}
}
#endregion
}
// Explicit interfaces implementation
public class ExplicitClientA : IGenericItems<IGenericItemA>, IGenericItems<IGenericItemB>
{
#region IGenericItems<IGenericItemA> Members
IEnumerable<IGenericItemA> IGenericItems<IGenericItemA>.Items
{
get
{
var collection = new List<IGenericItemA>();
collection.Add(new ConcreteA(1));
return collection;
}
}
#endregion
#region IGenericItems<IGenericItemB> Members
IEnumerable<IGenericItemB> IGenericItems<IGenericItemB>.Items
{
get
{
var collection = new List<IGenericItemB>();
collection.Add(new ConcreteB(1));
return collection;
}
}
#endregion
}
public class ExplicitClientB : ImplicitClientA, IGenericItems<IGenericItemB>, IGenericItems<IGenericItemC>
{
#region IGenericItems<IGenericItemB> Members
IEnumerable<IGenericItemB> IGenericItems<IGenericItemB>.Items
{
get
{
var collection = new List<IGenericItemB>();
collection.Add(new ConcreteB(2));
return collection;
}
}
#endregion
#region IGenericItems<IGenericItemC> Members
IEnumerable<IGenericItemC> IGenericItems<IGenericItemC>.Items
{
get
{
var collection = new List<IGenericItemC>();
collection.Add(new ConcreteC(2));
return collection;
}
}
#endregion
}
}
在此先感謝。