Are nesting classes and child classes the same?

What is the difference between Nestedand Childin the example below? Is this just a different syntax for the same thing?

class Parent
  class Nested
    ...
  end
end

class Child < Parent
  ...
end
+4
source share
2 answers

No , they are different.

Nested: the class "Processor" outside the computer can only be accessed as Computer :: Processor. Nesting provides context for the inner class (namespace). For the ruby ​​interpreter, Computer and Computer :: Processor are just two separate classes.

 class Computer
  class Processor # To create an object for this class, this is the syntax Computer::Processor.new. The Outer class provides context

: , / . / , Child.new/Parent.new

class Child < Parent

, Processor Computer::Processor, Processor . Child , Parent::Child ( ).

+5

:

  • Nested , , Parent. , @Jorg, (. Java).
  • Child Parent, , aka (is-a), Child < Parent. : Child Parent.

, , , Ruby.

  • . .
  • , class module.
  • :: .
  • ( a) class, . class C; ...; end, C, .
+1

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


All Articles