Multiple Inheritance in C#

后端 未结 15 1749
醉酒成梦
醉酒成梦 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.

提交回复
热议问题