How to simulate multiple inheritance without interfaces?

How can I simulate multiple inheritance in C # without using interfaces. I believe the interfaces are not designed for this task. I am looking for a more oriented "design template".

+3
source share
5 answers

ECMA-334, & sect; 8.9 Interfaces
  ...
  Interfaces can use multiple inheritance.

So, since C # (limited) support for "multiple inheritance" is coming, interfaces are the official way.

+2
source

, + , - mixins, , .

.: Mixins :

using System;

public interface ISwimmer{
}

public interface IMammal{
}

class Dolphin: ISwimmer, IMammal{
        public static void Main(){
        test();
                }
            public static void test(){
            var Cassie = new Dolphin();
                Cassie.swim();
            Cassie.giveLiveBirth();
                }
}

public static class Swimmer{
            public static void swim(this ISwimmer a){
            Console.WriteLine("splashy,splashy");
                }
}

public static class Mammal{
            public static void giveLiveBirth(this IMammal a){

        Console.WriteLine("Not an easy Job");
            }

}

splasshy,

+5

, , :

public class Base {}

public class SomeInheritance : Base {}

public class SomeMoreInheritance : SomeInheritance {}

public class Inheriting3 : SomeModeInheritance {}

, :

  • Base,
  • SomeInheritance
  • SomeMoreInheritance

, - . , , ( , ).

+2

, " mixin", .

+1
source

Since C # only supports one inheritance, I believe you need to add additional classes.

Are there any special reasons for not using interfaces? It is not clear from your description why the interfaces are not suitable.

0
source

Source: https://habr.com/ru/post/1767729/


All Articles