Multiple Inheritance in C#

后端 未结 15 1822
醉酒成梦
醉酒成梦 2020-11-22 03:03

Since multiple inheritance is bad (it makes the source more complicated) C# does not provide such a pattern directly. But sometimes it would be helpful to have this ability.

相关标签:
15条回答
  • 2020-11-22 03:08

    In my own implementation I found that using classes/interfaces for MI, although "good form", tended to be a massive over complication since you need to set up all that multiple inheritance for only a few necessary function calls, and in my case, needed to be done literally dozens of times redundantly.

    Instead it was easier to simply make static "functions that call functions that call functions" in different modular varieties as a sort of OOP replacement. The solution I was working on was the "spell system" for a RPG where effects need to heavily mix-and-match function calling to give an extreme variety of spells without re-writing code, much like the example seems to indicate.

    Most of the functions can now be static because I don't necessarily need an instance for spell logic, whereas class inheritance can't even use virtual or abstract keywords while static. Interfaces can't use them at all.

    Coding seems way faster and cleaner this way IMO. If you're just doing functions, and don't need inherited properties, use functions.

    0 讨论(0)
  • 2020-11-22 03:08

    If you can live with the restriction that the methods of IFirst and ISecond must only interact with the contract of IFirst and ISecond (like in your example)... you can do what you ask with extension methods. In practice, this is rarely the case.

    public interface IFirst {}
    public interface ISecond {}
    
    public class FirstAndSecond : IFirst, ISecond
    {
    }
    
    public static MultipleInheritenceExtensions
    {
      public static void First(this IFirst theFirst)
      {
        Console.WriteLine("First");
      }
    
      public static void Second(this ISecond theSecond)
      {
        Console.WriteLine("Second");
      }
    }
    

    ///

    public void Test()
    {
      FirstAndSecond fas = new FirstAndSecond();
      fas.First();
      fas.Second();
    }
    

    So the basic idea is that you define the required implementation in the interfaces... this required stuff should support the flexible implementation in the extension methods. Anytime you need to "add methods to the interface" instead you add an extension method.

    0 讨论(0)
  • 2020-11-22 03:09

    With C# 8 now you practically have multiple inheritance via default implementation of interface members:

    interface ILogger
    {
        void Log(LogLevel level, string message);
        void Log(Exception ex) => Log(LogLevel.Error, ex.ToString()); // New overload
    }
    
    class ConsoleLogger : ILogger
    {
        public void Log(LogLevel level, string message) { ... }
        // Log(Exception) gets default implementation
    }
    
    0 讨论(0)
  • 2020-11-22 03:13

    I created a C# post-compiler that enables this kind of thing:

    using NRoles;
    
    public interface IFirst { void FirstMethod(); }
    public interface ISecond { void SecondMethod(); }
    
    public class RFirst : IFirst, Role {
      public void FirstMethod() { Console.WriteLine("First"); }
    }
    
    public class RSecond : ISecond, Role {
      public void SecondMethod() { Console.WriteLine("Second"); }
    }
    
    public class FirstAndSecond : Does<RFirst>, Does<RSecond> { }
    

    You can run the post-compiler as a Visual Studio post-build-event:

    C:\some_path\nroles-v0.1.0-bin\nutate.exe "$(TargetPath)"

    In the same assembly you use it like this:

    var fas = new FirstAndSecond();
    fas.As<RFirst>().FirstMethod();
    fas.As<RSecond>().SecondMethod();
    

    In another assembly you use it like this:

    var fas = new FirstAndSecond();
    fas.FirstMethod();
    fas.SecondMethod();
    
    0 讨论(0)
  • 2020-11-22 03:14

    i know i know even though its not allowed and so on, sometime u actualy need it so for the those:

    class a {}
    class b : a {}
    class c : b {}
    

    like in my case i wanted to do this class b : Form (yep the windows.forms) class c : b {}

    cause half of the function were identical and with interface u must rewrite them all

    0 讨论(0)
  • 2020-11-22 03:17

    Consider just using composition instead of trying to simulate Multiple Inheritance. You can use Interfaces to define what classes make up the composition, eg: ISteerable implies a property of type SteeringWheel, IBrakable implies a property of type BrakePedal, etc.

    Once you've done that, you could use the Extension Methods feature added to C# 3.0 to further simplify calling methods on those implied properties, eg:

    public interface ISteerable { SteeringWheel wheel { get; set; } }
    
    public interface IBrakable { BrakePedal brake { get; set; } }
    
    public class Vehicle : ISteerable, IBrakable
    {
        public SteeringWheel wheel { get; set; }
    
        public BrakePedal brake { get; set; }
    
        public Vehicle() { wheel = new SteeringWheel(); brake = new BrakePedal(); }
    }
    
    public static class SteeringExtensions
    {
        public static void SteerLeft(this ISteerable vehicle)
        {
            vehicle.wheel.SteerLeft();
        }
    }
    
    public static class BrakeExtensions
    {
        public static void Stop(this IBrakable vehicle)
        {
            vehicle.brake.ApplyUntilStop();
        }
    }
    
    
    public class Main
    {
        Vehicle myCar = new Vehicle();
    
        public void main()
        {
            myCar.SteerLeft();
            myCar.Stop();
        }
    }
    
    0 讨论(0)
提交回复
热议问题