Multiple Inheritance in C#

后端 未结 15 1823
醉酒成梦
醉酒成梦 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:34

    Yes using Interface is a hassle because anytime we add a method in the class we have to add the signature in the interface. Also, what if we already have a class with a bunch of methods but no Interface for it? we have to manually create Interface for all the classes that we want to inherit from. And the worst thing is, we have to implement all methods in the Interfaces in the child class if the child class is to inherit from the multiple interface.

    By following Facade design pattern we can simulate inheriting from multiple classes using accessors. Declare the classes as properties with {get;set;} inside the class that need to inherit and all public properties and methods are from that class, and in the constructor of the child class instantiate the parent classes.

    For example:

     namespace OOP
     {
         class Program
         {
             static void Main(string[] args)
             {
                 Child somechild = new Child();
                 somechild.DoHomeWork();
                 somechild.CheckingAround();
                 Console.ReadLine();
             }
         }
    
         public class Father 
         {
             public Father() { }
             public void Work()
             {
                 Console.WriteLine("working...");
             }
             public void Moonlight()
             {
                 Console.WriteLine("moonlighting...");
             }
         }
    
    
         public class Mother 
         {
             public Mother() { }
             public void Cook()
             {
                 Console.WriteLine("cooking...");
             }
             public void Clean()
             {
                 Console.WriteLine("cleaning...");
             }
         }
    
    
         public class Child 
         {
             public Father MyFather { get; set; }
             public Mother MyMother { get; set; }
    
             public Child()
             {
                 MyFather = new Father();
                 MyMother = new Mother();
             }
    
             public void GoToSchool()
             {
                 Console.WriteLine("go to school...");
             }
             public void DoHomeWork()
             {
                 Console.WriteLine("doing homework...");
             }
             public void CheckingAround()
             {
                 MyFather.Work();
                 MyMother.Cook();
             }
         }
    
    
     }
    

    with this structure class Child will have access to all methods and properties of Class Father and Mother, simulating multiple inheritance, inheriting an instance of the parent classes. Not quite the same but it is practical.

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

    This is along the lines of Lawrence Wenham's answer, but depending on your use case, it may or may not be an improvement -- you don't need the setters.

    public interface IPerson {
      int GetAge();
      string GetName();
    }
    
    public interface IGetPerson {
      IPerson GetPerson();
    }
    
    public static class IGetPersonAdditions {
      public static int GetAgeViaPerson(this IGetPerson getPerson) { // I prefer to have the "ViaPerson" in the name in case the object has another Age property.
        IPerson person = getPerson.GetPersion();
        return person.GetAge();
      }
      public static string GetNameViaPerson(this IGetPerson getPerson) {
        return getPerson.GetPerson().GetName();
      }
    }
    
    public class Person: IPerson, IGetPerson {
      private int Age {get;set;}
      private string Name {get;set;}
      public IPerson GetPerson() {
        return this;
      }
      public int GetAge() {  return Age; }
      public string GetName() { return Name; }
    }
    

    Now any object that knows how to get a person can implement IGetPerson, and it will automatically have the GetAgeViaPerson() and GetNameViaPerson() methods. From this point, basically all Person code goes into IGetPerson, not into IPerson, other than new ivars, which have to go into both. And in using such code, you don't have to be concerned about whether or not your IGetPerson object is itself actually an IPerson.

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

    You could have one abstract base class that implements both IFirst and ISecond, and then inherit from just that base.

    0 讨论(0)
提交回复
热议问题