Using generics in abstract classes

前端 未结 7 1513
眼角桃花
眼角桃花 2020-12-05 10:17

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:

public class AbstractC         


        
相关标签:
7条回答
  • 2020-12-05 10:52
    1. You need to mark AbstractClass abstract, because it contains abstract property

    2. Specify the generic type in the AbstractClass declaration

    3. Implement abstract property with 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; }
    }
    
    
    0 讨论(0)
  • 2020-12-05 10:54

    You need to define T like so

    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; }
    }
    
    0 讨论(0)
  • 2020-12-05 11:01
    • You need to declare the type T.
    • You need to declare the class AbstractClass as abstract.
    • You need to use the override keyword.

    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; }
    }
    
    0 讨论(0)
  • 2020-12-05 11:05

    You need the declaration on the class as well, to know what type T is:

    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:

    public class AbstractClass<T> where T : IWidgets
    
    0 讨论(0)
  • 2020-12-05 11:08

    You need to make AbstractClass generic

    public class AbstractClass<T> {
      ...
    }
    
    public class Container : AbstractClass<Widgets> { ...
    }
    
    0 讨论(0)
  • 2020-12-05 11:12

    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.

    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();
        }
    }
    }
    
    0 讨论(0)
提交回复
热议问题