Multiple Inheritance in Ruby?

前端 未结 4 556
鱼传尺愫
鱼传尺愫 2021-02-01 15:04

I thought that Ruby only allowed single inheritance besides mixin. However, when I have class Square that inherits class Thing, Thing in t

相关标签:
4条回答
  • 2021-02-01 15:46

    No, multi inheritance means one class have more than one parent class. For example in ruby you can have that behavior with modules like:

    class Thing
      include MathFunctions
      include Taggable
      include Persistence
    end
    

    So in this example Thing class would have some methods from MathFunctions module, Taggable and Persistence, that wouldn't be possible using simple class inheritance.

    0 讨论(0)
  • 2021-02-01 15:50

    If class B inherits from class A, then instances of B have the behaviors of both class A and class B

    class A
    end
    
    class B < A
      attr_accessor :editor
    end
    

    Ruby has single inheritance, i.e. each class has one and only one parent class. Ruby can simulate multiple inheritance using Modules(MIXINs)

    module A
      def a1
      end
    
      def a2
      end
    end
    
    module B
      def b1
      end
    
      def b2
      end
    end
    
    class Sample
      include A
      include B
    
      def s1
      end
    end
    
    
    samp=Sample.new
    samp.a1
    samp.a2
    samp.b1
    samp.b2
    samp.s1
    

    Module A consists of the methods a1 and a2. Module B consists of the methods b1 and b2. The class Sample includes both modules A and B. The class Sample can access all four methods, namely, a1, a2, b1, and b2. Therefore, you can see that the class Sample inherits from both the modules. Thus you can say the class Sample shows multiple inheritance or a mixin.

    0 讨论(0)
  • 2021-02-01 16:05

    I think you are taking the meaning of multiple inheritance in a wrong way. Probably what you have in mind as multiple inheritance is like this:

    class A inherits class B
    class B inherits class C
    

    If so, then that is wrong. That is not what multiple inheritance is, and Ruby has no problem with that. What multiple inheritance really means is this:

    class A inherits class B
    class A inherits class C
    

    And you surely cannot do this in Ruby.

    0 讨论(0)
  • 2021-02-01 16:06

    Multiple inheritance - This is absolutely not possible in ruby not even with modules.

    multilevel inheritance - This is what is possible even with the modules.

    Why ?

    Modules acts as an absolute superclasses to the class that includes them.

    Ex1 :

    class A
    end
    class B < A
    end
    

    This is a normal inheritance chain, and you can check this by ancestors.

    B.ancestors => B -> A -> Object -> ..

    Inheritance with Modules -

    Ex2 :

    module B
    end
    class A
      include B
    end
    

    inheritance chain for above example -

    B.ancestors => B -> A -> Object -> ..

    Which is exactly same as a Ex1. That means all the methods in module B can override the methods in A, And it acts as a real class inheritance.

    Ex3 :

    module B
      def name
         p "this is B"
      end
    end
    module C
      def name
         p "this is C"
      end
    end
    class A
      include B
      include C
    end
    

    A.ancestors => A -> C -> B -> Object -> ..

    if you see the last example even though two different modules are included they are in a single inheritance chain where B is a superclass of C, C is a superclass of A.

    so,

    A.new.name => "this is C"
    

    if you remove the name method from module C, above code will return "this is B". which is same as inheriting a class.

    so,

    At any point there is only one multilevel inheritance chain in ruby, which nullifies having multiple direct parent to the class.

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