ruby继承_Ruby继承

骑猪看日落 2023-03-05 07:29 291阅读 0赞

ruby继承

Ruby中的继承 (Inheritance in Ruby)

Inheritance is a feature of Object Oriented languages in which new classes are derived from existing classes and resulting in the formation of a hierarchy of classes. The derived class is often called as child class and the existing class is termed as parent class. Inheritance provides code reusability which increases the human efficiency to write codes on the platform.

继承是面向对象语言的一种功能,其中新类从现有类派生,并导致形成类层次结构。 派生类通常称为子类 ,现有类称为父类 。 继承提供了代码可重用性,从而提高了人员在平台上编写代码的效率。

Ruby is an Object Oriented language, thus it supports the major feature of Inheritance. We can also explain inheritance via an example of two classes namely A and B.

Ruby是一种面向对象的语言,因此它支持Inheritance的主要功能。 我们还可以通过两个类AB的示例来解释继承。

Let us define these two classes in ruby using its syntax:

让我们使用其语法在ruby中定义这两个类:

  1. class A
  2. #class methods
  3. end
  4. class B
  5. #class methods
  6. end

If we want to provide inheritance on class B, then the syntax will be changed as,

如果我们要在类B上提供继承 ,则语法将更改为:

  1. class A
  2. #class methods
  3. end
  4. class B < A
  5. #class methods
  6. end

In the above syntax that we have used the “ symbol to inherit a class. Now, if the object of class B is created then it will also be able to use the data members and member methods of class A. This provides code reusability as now we don’t have to define methods which are already declared in class A, in class B as well. There are two classes possible after inheritance.

在上面的语法中,我们使用了“符号来继承一个类。现在,如果创建了类B的对象,那么它也将能够使用类A的数据成员和成员方法。这提供了现在的代码可重用性。我们不必定义已经在类A和类B中声明的方法,继承后可能有两个类。

  1. Super Class: Super class is the Parent class whose methods are inherited. It can also be termed as Base class.

    超级类 :超级类是其方法被继承的Parent类。 也可以称为基类。

  2. Sub Class: Sub class is often termed as Derived or Child class. Sub class derives the methods and variables of Base class or Parent class.

    子类 :子类通常被称为派生类或子类。 子类派生基类或父类的方法和变量。

Ruby supports only single level inheritance which means that a child class can have only one base class or parent class. It disallows Multi-level inheritance which means that if we want to make multiple parent classes of a single child class then it is not possible. Multiple inheritances are restricted because it creates ambiguity error or you can say that it creates multiple paths if the method name is same in both parent classes and the compiler could not decide or choose the right path.

Ruby仅支持单级继承,这意味着子类只能具有一个基类或父类。 它不允许多级继承,这意味着如果我们要使单个子类具有多个父类,则不可能。 多重继承受到限制,因为它会产生歧义错误,或者如果两个父类中的方法名称相同并且编译器无法决定或选择正确的路径,则可以说它会创建多个路径。

Every class which is defined in Ruby platform has a default parent class. Before Ruby 1.9 version, every class has the parent class known as “Object class” by default but after Ruby 1.9 version, the parent class or the superclass of every class is “Basic Object class” by default.

Ruby平台中定义的每个类都有一个默认的父类。 在Ruby 1.9之前,每个类的父类默认情况下都称为“对象类”,而在Ruby 1.9以后,每个类的父类或超类的默认情况下都为“基本对象类”。

Let us understand the practical implementation of Inheritance with the help of the following example,

让我们借助以下示例了解继承的实际实现:

  1. =begin
  2. Ruby program to demonstrate Inheritance.
  3. =end
  4. class ClassA
  5. def Show
  6. puts "Welcome to IncludeHelp"
  7. end
  8. def Message
  9. puts "Enter your name: "
  10. nm=gets.chomp
  11. puts "Hello #{nm}, I hope you are doing great"
  12. end
  13. end
  14. class ClassB<ClassA
  15. def Hello
  16. puts "Hello World!"
  17. end
  18. end
  19. ob1=ClassB.new
  20. ob1.Show
  21. ob1.Message
  22. ob1.Hello

Output

输出量

  1. Welcome to IncludeHelp
  2. Enter your name:
  3. Hrithik
  4. Hello Hrithik, I hope you are doing great
  5. Hello World!

You can observe in the above code that, ClassB is the child class of Base class ClassA. The object of ClassB can access the methods of ClassA.

您可以在上面的代码中观察到, ClassB是基类ClassA的子类。 ClassB的对象可以访问ClassA的方法。

翻译自: https://www.includehelp.com/ruby/inheritance.aspx

ruby继承

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读