C# 在抽象类中使用泛型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2359540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using generics in abstract classes
提问by thaBadDawg
I'm working on an abstract class where the implementing class needs to implement a list of T. The problem is that this doesn't work:
我正在研究一个抽象类,其中实现类需要实现 T 的列表。问题是这不起作用:
public class AbstractClass
{
public int Id { get; set; }
public int Name { get; set; }
public abstract List<T> Items { get; set; }
}
public class Container : AbstractClass
{
public List<Widgets> Items { get; set; }
}
I'm sure that there is an obvious answer that I'm missing, and I know that I can build an abstract base type to put in the list, but when I use my Linq command to build the list, the abstract type (ItemBase) doesn't play nicely with the .ToList() method. Is what I'm trying to do so unique?
我确定我缺少一个明显的答案,并且我知道我可以构建一个抽象基类型以放入列表,但是当我使用我的 Linq 命令构建列表时,抽象类型 (ItemBase ) 不能很好地与 .ToList() 方法配合使用。我正在尝试做的事情很独特吗?
采纳答案by Nick Craver
You need the declaration on the class as well, to know what type T
is:
您还需要类的声明,以了解是什么类型T
:
public abstract class AbstractClass<T>
{
public int Id { get; set; }
public int Name { get; set; }
public abstract List<T> Items { get; set; }
}
public class Container : AbstractClass<Widgets>
{
public List<Widgets> Items { get; set; }
}
You can also restrict what T can be, like say it must implement IWidgets
:
您还可以限制 T 可以是什么,比如说它必须实现IWidgets
:
public class AbstractClass<T> where T : IWidgets
回答by JaredPar
You need to make AbstractClass
generic
你需要AbstractClass
通用
public class AbstractClass<T> {
...
}
public class Container : AbstractClass<Widgets> { ...
}
回答by Reed Copsey
You need to specify the type in the abstract class:
您需要在抽象类中指定类型:
public class AbstractClass<T>
{
public int Id { get; set; }
public int Name { get; set; }
public abstract List<T> Items { get; set; }
}
public class Container : AbstractClass<Widgets>
{
public List<Widgets> Items { get; set; }
}
回答by JDMX
You need to define T like so
你需要像这样定义 T
public class AbstractClass<T>
{
public int Id { get; set; }
public int Name { get; set; }
public abstract List<T> Items { get; set; }
}
public class Container : AbstractClass<Widget>
{
public List<Widgets> Items { get; set; }
}
回答by Mark Byers
- You need to declare the type T.
- You need to declare the class AbstractClass as abstract.
- You need to use the override keyword.
- 您需要声明类型 T。
- 您需要将 AbstractClass 类声明为抽象类。
- 您需要使用 override 关键字。
Try this:
尝试这个:
public class Widgets { }
public abstract class AbstractClass<T>
{
public int Id { get; set; }
public int Name { get; set; }
public abstract List<T> Items { get; set; }
}
public class Container : AbstractClass<Widgets>
{
public override List<Widgets> Items { get; set; }
}
回答by Stanislav Basovník
You need to mark AbstractClass
abstract
, because it contains abstract propertySpecify the generic typein the
AbstractClass
declarationImplement abstract property with
override
您需要标记 AbstractClass
abstract
,因为它包含抽象属性在声明中指定泛型类型
AbstractClass
实现抽象属性
override
public abstract class AbstractClass<T>
{
public int Id { get; set; }
public int Name { get; set; }
public abstract List<T> Items { get; set; }
}
public class Container : AbstractClass<Widgets>
{
public override List<Widgets> Items { get; set; }
}
回答by Humayoun Kabir
You are missing to define abstract keyword for your AbstractClass class and also you need to add override keyword for Container Class Items property. I am giving the example code which will run for sure.
您缺少为 AbstractClass 类定义抽象关键字,并且您还需要为容器类项属性添加覆盖关键字。我给出了肯定会运行的示例代码。
namespace AbstractGeneric
{
public abstract class AbstractClass<T>
{
public int Id { get; set; }
public int Name { get; set; }
public abstract List<T> Items { get; set; }
}
public class Widgets
{
public int ID { get; set; }
public string Name { get; set; }
}
public class Container : AbstractClass<Widgets>
{
public override List<Widgets> Items { get; set; }
public Container()
{
Items = new List<Widgets>();
}
}
class Program
{
static void Main(string[] args)
{
Container c = new Container();
c.Items.Add(new Widgets() { ID = 1, Name = "Sample Widget 1" });
c.Items.Add(new Widgets() { ID = 2, Name = "Sample Widget 2" });
foreach (Widgets item in c.Items)
{
Console.WriteLine(item.ID + " : " + item.Name);
}
Console.ReadLine();
}
}
}