简体   繁体   English

在抽象类中使用泛型

[英]Using generics in abstract classes

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.我确定我缺少一个明显的答案,并且我知道我可以构建一个抽象基类型以放入列表,但是当我使用我的 Linq 命令构建列表时,抽象类型 (ItemBase ) 不能很好地与 .ToList() 方法配合使用。 Is what I'm trying to do so unique?我正在尝试做的事情很独特吗?

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 override 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
  • You need to declare the type T.您需要声明类型 T。
  • You need to declare the class AbstractClass as abstract.您需要将 AbstractClass 类声明为抽象类。
  • You need to use the override keyword.您需要使用 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; }
}

You need to make AbstractClass generic您需要使AbstractClass泛型

public class AbstractClass<T> {
  ...
}

public class Container : AbstractClass<Widgets> { ...
}
  1. You need to mark AbstractClass abstract , because it contains abstract property您需要标记 AbstractClass abstract ,因为它包含抽象属性

  2. Specify the generic type in the AbstractClass declarationAbstractClass声明中指定泛型类型

  3. Implement abstract property with override使用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; }
}

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; }
}

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; }
}

You are missing to define abstract keyword for your AbstractClass class and also you need to add override keyword for Container Class Items property.您缺少为 AbstractClass 类定义抽象关键字,并且您还需要为容器类项属性添加覆盖关键字。 I am giving the example code which will run for sure.我给出了肯定会运行的示例代码。

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();
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM