日期:2014-05-17 浏览次数:20844 次
//父类
public class Base
{
public virtual object GetList()
{
return null;
}
}
//子类A
public class A : Base
{
public override object GetList()
{
//返回List<A>
List<A> aList = new List<A>();
return aList;//会不会自动转成Object类型
}
}
//子类B
public class B : Base
{
public override object GetList()
{
List<B> bList = new List<B>();
return bList;//会不会转成Object类型
}
}
//上下文
public class Context
{
Base b;
public Context(Base b)
{
this.b = b;
}
public object GetList()
{
return b.GetList();
}
}
//客户端
public class Client
{
public void Test()
{
Context ct = new Context(new A());
List<A> aList = ct.GetList() as List<A>;//会不会有性能损失,数据会不会转出错了
List<B> bList = new Context(new B()).GetList() as List<B>;
}
}