C# SomeList<;T>;:列表<;T>;can';不能被选为列表<;T>;?
请参阅Main()中的注释。为什么我不能执行以下操作C# SomeList<;T>;:列表<;T>;can';不能被选为列表<;T>;?,c#,generics,inheritance,interface,C#,Generics,Inheritance,Interface,请参阅Main()中的注释。为什么我不能执行以下操作 public class SomeList<T> : List<T> { public SomeList(List<T> existing) { foreach (var item in existing) Add(item); } public override string ToString() {
public class SomeList<T> : List<T>
{
public SomeList(List<T> existing)
{
foreach (var item in existing)
Add(item);
}
public override string ToString()
{
return "I'm a better list.";
}
}
internal interface IReadStuff<T>
{
List<T> ReadStuff();
}
public class ReaderStrategy<Foo> : IReadStuff<Foo>
{
public List<Foo> ReadStuff()
{
return new List<Foo>();
}
}
public class Foo {}
public class Main
{
public Main()
{
var reader = new ReaderStrategy<Foo>();
// This works, but type is List<Foo>, not SomeList<Foo>
List<Foo> aList = reader.ReadStuff();
// This does not compile, but is what I want to do:
SomeList<Foo> aBetterList = reader.ReadStuff();
// This compiles, but always generates null for aBetterList:
SomeList<Foo> anotherBetterList = reader.ReadStuff() as SomeList<Foo>;
// This is funky but works:
SomeList<Foo> works = new SomeList<Foo>(reader.ReadStuff());
}
}
public类SomeList:List
{
公共SomeList(现有列表)
{
foreach(现有中的var项目)
增加(项目);
}
公共重写字符串ToString()
{
返回“我是一个更好的列表。”;
}
}
内部接口IReadStuff
{
列出ReadStuff();
}
公共类读者策略:IReadStuff
{
公共列表ReadStuff()
{
返回新列表();
}
}
公共类Foo{}
公共班机
{
公用干管()
{
var reader=new ReaderStrategy();
//这是可行的,但类型是List,而不是SomeList
List aList=reader.ReadStuff();
//这不是编译,而是我想做的:
SomeList aBetterList=reader.ReadStuff();
//这将编译,但始终为aBetterList生成null:
SomeList anotherBetterList=reader.ReadStuff()作为SomeList;
//这很时髦,但很有效:
SomeList works=新的SomeList(reader.ReadStuff());
}
}
我正在努力理解如何对继承类型使用泛型。我需要上述功能,因为我想扩展List
的功能,这是一种特殊的方式,例如,请参见SomeList覆盖ToString()
。但是,我想使用.Net genericList
保留我的工厂策略。有没有办法让这一切顺利进行
编辑
我添加了一个接受List
并添加到SomeList
的构造函数。这看起来不自然,但很有效。这是一个昂贵的操作,尤其是当列表
很大时
我的问题标题不是最好的,我一直在争取的是一个显示更好方法的示例。reader.ReadStuff()
返回List
-但您试图将其分配给继承自List
的类型为SomeList
的对象。这不起作用,因为List
不是一个SomeList
——相反
想想看,从ReadStuff()返回List
对象是合法的
-然后您尝试访问此对象上仅在SomeList
上可用的功能-这将中断,这就是OOP不允许您这样做的原因-可以在需要父类实例的情况下使用子类实例,但不能在需要子类的情况下使用父类
返回问题标题:SomeList
:List
不能转换为List
?是的,这是可能的,但是您正在尝试将List
强制转换为SomeList
reader.ReadStuff()
返回List
——但您正在尝试将其分配给继承自List
的SomeList
类型的对象。这不起作用,因为List
不是一个SomeList
——相反
想想看,从ReadStuff()返回List
对象是合法的
-然后您尝试访问此对象上仅在SomeList
上可用的功能-这将中断,这就是OOP不允许您这样做的原因-可以在需要父类实例的情况下使用子类实例,但不能在需要子类的情况下使用父类
返回问题标题:SomeList
:List
不能转换为List
?是的,这是可能的,但您正在尝试将List
强制转换为SomeList
SomeList的所有实例都是List的实例。但是,并非列表的所有实例都是SomeList的实例。这就是第二个作业正在做的。reader.ReadStuff()返回一个列表,而不是SomeList。希望这有帮助 SomeList的所有实例都是List的实例。但是,并非列表的所有实例都是SomeList的实例。这就是第二个作业正在做的。reader.ReadStuff()返回一个列表,而不是SomeList。希望这有帮助 在您的示例中,您不是将SomeList
的实例强制转换为List
,而是尝试将列表
强制转换为SomeList
。你从不太具体到更具体,这是行不通的。它应该反过来工作。在您的示例中,您不是将SomeList
的实例强制转换为List
,而是尝试将List
强制转换为SomeList
。你从不太具体到更具体,这是行不通的。它应该反过来工作。更改此代码
SomeList<Foo> aBetterList = reader.ReadStuff()
to
SomeList<Foo> aBetterList = reader.ReadStuff() as SomeList<Foo>;
before using
if(aBetterList !=null) {}
SomeList aBetterList=reader.ReadStuff()
到
SomeList aBetterList=reader.ReadStuff()作为SomeList;
使用前
如果(aBetterList!=null){}
更改此代码
SomeList<Foo> aBetterList = reader.ReadStuff()
to
SomeList<Foo> aBetterList = reader.ReadStuff() as SomeList<Foo>;
before using
if(aBetterList !=null) {}
SomeList aBetterList=reader.ReadStuff()
到
SomeList aBetterList=reader.ReadStuff()作为SomeList;
使用前
如果(aBetterList!=null){}
除了ToString()
我在列表中没有看到任何虚拟方法,因此从中继承通常不是一个好主意。除了ToString()
我在列表中没有看到任何虚拟方法,因此从中继承通常不是一个好主意。