How to use Inheritance when using Generic Constraints

后端 未结 3 801
梦毁少年i
梦毁少年i 2021-02-06 07:33

I\'m struggling with some Generic constraint issues when trying to implement a library that allows inheritance and hoping someone can help.

I\'m trying to build up a cl

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-06 08:12

    Here is the same thing as Enigmativity’s answer, but with all the redundant constraints removed. This still compiles, it doesn’t have any generic recursion in it, and as far as I can tell is still as type-safe as was required. Enigmativity, what am I missing? :)

    public class Generic_Element { }
    
    public class Generic_Visit
    {
        public E Element { get; set; }
    }
    
    /// Collection of Visits
    public class Generic_Route
    {
        public List Visits { get; set; }
        public Double Distance { get; set; }
    }
    
    /// Collection of Routes
    public class Generic_Solution
        where R : Generic_Route
    {
        public List Routes { get; set; }
    
        public Double Distance
        {
            get
            {
                return this.Routes.Select(r => r.Distance).Sum();
            }
        }
    }
    
    public class Generic_Tsp_Element : Generic_Element
    {
    }
    
    /// Visit to a Generic_Element
    public class Generic_Tsp_Visit : Generic_Visit
    {
        public Double Time { get; set; }
    }
    
    /// Collection of Visits
    public class Generic_Tsp_Route : Generic_Route
        where V : Generic_Tsp_Visit
    {
        public Double Time
        {
            get
            {
                return this.Visits.Select(v => v.Time).Sum();
            }
        }
    }
    
    /// Collection of Routes
    public class Generic_Tsp_Solution : Generic_Solution
        where R : Generic_Tsp_Route
        where V : Generic_Tsp_Visit
    {
        public Double Time
        {
            get
            {
                return this.Routes.Select(r => r.Time).Sum();
            }
        }
    }
    
    public class Concrete_Tsp_Element : Generic_Tsp_Element { }
    
    public class Concrete_Tsp_Visit : Generic_Tsp_Visit { }
    
    public class Concrete_Tsp_Route : Generic_Tsp_Route { }
    
    public class Concrete_Tsp_Solution : Generic_Tsp_Solution { }
    
    public class Generic_Vrp_Element : Generic_Element
    {
    }
    
    /// Visit to a Generic_Element
    public class Generic_Vrp_Visit : Generic_Visit
    {
        public Double Capacity { get; set; }
    }
    
    /// Collection of Visits
    public class Generic_Vrp_Route : Generic_Route
        where V : Generic_Vrp_Visit
    {
        public Double Capacity
        {
            get
            {
                return this.Visits.Select(v => v.Capacity).Sum();
            }
        }
    }
    
    /// Collection of Routes
    public class Generic_Vrp_Solution : Generic_Solution
        where R : Generic_Vrp_Route
        where V : Generic_Vrp_Visit
    {
        public Double Capacity
        {
            get
            {
                return this.Routes.Select(r => r.Capacity).Sum();
            }
        }
    }
    
    public class Concrete_Vrp_Element : Generic_Vrp_Element { }
    
    public class Concrete_Vrp_Visit : Generic_Vrp_Visit { }
    
    public class Concrete_Vrp_Route : Generic_Vrp_Route { }
    
    public class Concrete_Vrp_Solution : Generic_Vrp_Solution { }
    

提交回复
热议问题