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中定义这两个类:

    class A
#class methods
end
class B
#class methods
end

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

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

    class A
#class methods
end
class B < A
#class methods
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,

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

=begin
Ruby program to demonstrate Inheritance.
=end
class ClassA
def Show
puts "Welcome to IncludeHelp"
end
def Message
puts "Enter your name: "
nm=gets.chomp
puts "Hello #{nm}, I hope you are doing great"
end
end
class ClassB<ClassA
def Hello
puts "Hello World!"
end
end
ob1=ClassB.new
ob1.Show
ob1.Message
ob1.Hello

Output

输出量

Welcome to IncludeHelp
Enter your name:
Hrithik
Hello Hrithik, I hope you are doing great
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继承

ruby继承_Ruby继承相关推荐

  1. php 魔术方法 多继承,day23:单继承多继承菱形继承__init__魔术方法

    原文:https://www.cnblogs.com/libolun/p/13434675.html 单继承 关于继承的一些基本概念 1.什么是子类?什么是父类?如果一个类继承另外一个类,该类叫做子类 ...

  2. java 父类是抽象类 定义private的属性 子类继承_java继承、多态、接口、抽象类定义及应用...

    继承:关键字extends java 中继承是单继承 允许多个子类继承一个父类,不允许一个子类继承多个父类. 继承是继承父类的所有属性和方法,但并不是所有的属性和方法都可被子类调用: 子类继承父类是必 ...

  3. 菱形继承,多继承,虚继承、虚表的内存结构全面剖析(逆向分析基础)

    // 声明:以下代码均在Win32_Sp3   VC6.0_DEBUG版中调试通过.. 在逆向还原代码的时候,必须得掌握了菱形继承,多继承,虚继承虚函数的内存虚表结构.所以,这篇文章献给正在学习C++ ...

  4. java多态和继承_Java 继承与多态的深入理解

    Java 继承与多态的深入理解 1.  什么是继承,继承的特点? 子类继承父类的特征和行为,使得子类具有父类的各种属性和方法.或子类从父类继承方法,使得子类具有父类相同的行为. 特点:在继承关系中,父 ...

  5. 借用构造函数 组合继承 拷贝继承 总结继承

    借用构造函数 <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF ...

  6. C++虚继承(八) --- 虚继承与继承的差异

    前面一篇文章,说明了在C++ 虚继承对基类构造函数调用顺序的影响.经过仔细推敲,发现没有彻底说清楚虚继承与普通继承之间的关系.所以用下面的文字再说明一下. 首先,重复一下虚拟继承与普通继承的区别有: ...

  7. C++ 面向对象(一)继承:继承、对象切割、菱形继承、虚继承、继承与组合

    目录 继承 继承的概念 继承方式 基类与派生类的赋值转换 作用域与隐藏 派生类的默认成员函数 友元与静态成员 友元 静态成员 多继承 菱形继承 虚继承 继承和组合 什么是组合 如何选择组合和继承 继承 ...

  8. c++ 虚继承与继承的差异

    前面一篇文章,说明了在C++ 虚继承对基类构造函数调用顺序的影响.经过仔细推敲,发现没有彻底说清楚虚继承与普通继承之间的关系.所以用下面的文字再说明一下. 首先,重复一下虚拟继承与普通继承的区别有: ...

  9. JavaScript面向对象——理解构造函数继承(类继承)

    JavaScript面向对象--理解构造函数继承(类继承) 构造函数式继承(类继承) function SuperClass(id) {// 引用类型公有属性this.books = ['JavaSc ...

最新文章

  1. lisp直线连接圆象限电_用小学数学知识解释,为什么井盖是圆的?
  2. flutter - 如何在 dart/flutter 中收听流值
  3. 抖音访问太频繁-设备注册分析
  4. PHP中的__toString方法(实现JS里的链式操作)
  5. mysql 更新 字段 递增_MySQL使用递增变量更新字段
  6. cadence 旋转快捷键_cadence原理图快捷键
  7. Pytorch数据读取(Dataset, DataLoader, DataLoaderIter)
  8. 在flash cs3场景中插入按钮以后,无法测试场景,删除按钮以后又可以测试?可能是按钮中某个帧上的文本对象的字体错了
  9. android程序中关于webview加载html文件
  10. word2003邮件合并后保留小数点位数问题
  11. 魅族路由器(极速版)刷老毛子(padavad)固件-全网最详细教程
  12. 使用“for”循环遍历字典
  13. 使用jQuery,写一个简单的轮播图,实现切换功能!
  14. JAVA中的->是什么意思?
  15. bak文件转换成sql文件_数据库bak文件转sql
  16. 如何用Python自动检测微信中谁把你拉黑了?
  17. 上海市关于开展2023年度高新技术企业认定管理相关工作的通知
  18. 将统一标识的SCV文件批量合成excel文件
  19. 【FPGA学习】HDLbits练习(持续更新中。。。。)
  20. 校园招聘iOS开发岗位面试题集锦(2017)

热门文章

  1. html jq 控制显示密码,js、jquery分别实现点击密码输入框密码显示和隐藏
  2. rocketmq java例子_SpringBoot和RocketMQ的简单实例
  3. c++ file* 句柄泄漏_C++核心指南:P.8 勿泄漏任务资源
  4. scala通过JDBC进行数据库操作
  5. Docker Dockerfile详解
  6. Problem I: 函数---判断某年某月某日是这一年中的第几天
  7. C# Collection was modified;enumeration operation may not execute
  8. PostgreSQL 并行查询概述
  9. 使用Mybatis Generator结合Ant脚本快速自动生成Model、Mapper等文件的方法
  10. 原创 通过PEB获得进程路径 (附完整工程)